Node js function returns [object Object] instead of string value

I am comfortable with java script and node js, I am trying to get the value from the MySQL database, and the return value is [object Object] instead of the string. I really did not find the answer on the Internet, what is the problem. I hope someone here can help. the string value is [object Object].

here is my function

exports.getAllIdInfo= function(dbConnection, tables ,id , callback){ var tableName= tables[i]; var tableVariable = tableName; var myQuery = 'SELECT time, ' + tableVariable + ' FROM ' + tableName + ' WHERE id= ' + id; var query = dbConnection.query(myQuery, function (err, row, result) { console.log(query.sql); if (err) { console.log("getAllGoodIds error"); console.error(err); return; } console.log("row is: " + row); callback(row); }); }; 
+9
source share
2 answers

[object Object] appears in the log when there is an object with keys and values. You can access the properties of an object with dotted notation (.), For example,

 objectName.propertyName 

If ProperyName is another object, it will still return [Object], and therefore you need to look for another property in this. Properties may also contain methods (functions). If you want to get a string version of an object to compare them, for example, use

 JSON.stringify(objectName); 

When using console.log with a node, when you have a deeply nested object, you will not be able to view the contents of the nested object. In this case you can use:

 console.log(util.inspect(objectName, false, null)); 

To view the entire object. Although you need to specify util in the file.


Maybe you have something like:

 const myObject = { hello: 'world' }; console.log('My object: '+myObject); 

The problem is that it converts myObject to a string in the console, for example using myObject.toString() . In this case, you can simplify for yourself and separate it as follows:

 const myObject = { hello: 'world' }; console.log('My object:', myObject); 

And now the console can interpret myObject and display it beautifully.

+15
source

I also ran into this problem by running the following code on the node.js terminal in combination with "watchman-make" (watchman-make: see comments in the first answer at https://www.quora.com/What-IDEs-are -available-for-node-js-development-on-Linux ).

The following code (with the image of node.js) shows the points made in the accepted answer / comments:

 function arrayToList(array) { var list = {}; for (var i = array.length - 1; i >= 0; i--) { list = {value: array[i], rest: list}; } return list; }; console.log(arrayToList( [1, 2, 3, 4, 5] )); // { value: 1, // rest: { value: 2, rest: { value: 3, rest: [Object] } } } // '[Object]' ? // http://stackoverflow.com/questions/34264800/node-js-function-return-object-object-instead-of-a-string-value var obj = arrayToList([1, 2, 3, 4, 5]); console.log('%j', obj); // {"value":1,"rest":{"value":2,"rest":{"value":3,"rest":{"value":4,"rest":{"value":5,"rest":null}}}}} console.log(JSON.stringify(obj)); // {"value":1,"rest":{"value":2,"rest":{"value":3,"rest":{"value":4,"rest":{"value":5,"rest":null}}}}} 
0
source

Source: https://habr.com/ru/post/1238098/


All Articles