17

i am quiet new to java script and node js, i am trying to get a value from a MySQL DB, and the return value is [object Object] instead of a string. i didn't really found any answer online what is the problem. i hope someone here could help. the row 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);
        });
};
Dan The Man
  • 1,835
  • 6
  • 30
  • 50
  • If by return value you mean the callback argument "row", it's probably an object containing key-value pairs in the row (I can only guess since I don't know which sql library you are using), which should be what you actually want: if it's an object, you can get values from it by invoking its properties, such as row.id and row.name or whatever columns you have in your table. – Octav Zlatior Dec 14 '15 at 10:37
  • 1
    what library are you using? node-mysql? – madox2 Dec 14 '15 at 10:37
  • 2
    If you want to see how the object is structured, try calling console.log(row) (without appending any string to it) or convert it to string (you can use JSON.stringify(row) ) – Octav Zlatior Dec 14 '15 at 10:37
  • (or log it as a separate argument: `console.log('row is:', row)` (notice the comma)) – robertklep Dec 14 '15 at 10:40
  • 1
    btw. do not concatenate query like this. it is vulnerable to sql injection – madox2 Dec 14 '15 at 10:44

4 Answers4

28

[object Object] occurs in the log when there is an object with keys and values. You can access properties in an object wth dot notation (.) e.g

objectName.propertyName

If properyName is another object it will still return [object Object] and so you need to look for another property within that. Properties could also contain methods (functions). If you want to get the string version of an object in order to compare them for example, then use

JSON.stringify(objectName);

When using console.log with node and you have a deeply nested object, you may not be able to view the nested object contents. In that case you can use:

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

To view the entirety of the object. Although you must require util in the file.


Maybe you have something like:

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

The problem with this is that it converts myObject to a string in the console e.g. using myObject.toString(). In this case, you can make it easier for yourself and separate it like this:

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

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

George
  • 2,330
  • 3
  • 15
  • 36
  • 6
    This doesn't hold true for Node's [`console.log()`](https://nodejs.org/api/console.html#console_console_log_data), which will (usually) use [`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options) on each argument instead of simply calling `.toString()` on it. FWIW, if you want to log JSON, you can also use `console.log('%j', obj)` – robertklep Dec 14 '15 at 11:00
  • 1
    Hmmm, I did not know about the JSON thing, nice :) – Octav Zlatior Dec 14 '15 at 11:16
1

I also encountered this issue, running the following code in a node.js terminal in conjunction with "watchman-make" (watchman-make: see comments in first answer at https://www.quora.com/What-IDEs-are-available-for-node-js-development-on-Linux).

The following code (with node.js output shown) illustrates 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}}}}}
Victoria Stuart
  • 4,610
  • 2
  • 44
  • 37
1

Use one of the options:

console.log("%o", yourObjName)
console.log(JSON.stringify(yourObjName))
console.log(JSON.stringify(yourObjName, null, 3))
0

I think JSON.stringify(yourObjName) is the perfect solution. you may apply it in your code segment.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '22 at 02:26