简体   繁体   English

node js函数返回[object Object]而不是字符串值

[英]node js function return [object Object] instead of a string value

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.我对 java 脚本和节点 js 很陌生,我试图从 MySQL 数据库中获取一个值,返回值是 [object Object] 而不是字符串。 i didn't really found any answer online what is the problem.我真的没有在网上找到任何答案是什么问题。 i hope someone here could help.我希望这里有人可以提供帮助。 the row value is [object Object] .行值为 [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);
        });
};

[object Object] occurs in the log when there is an object with keys and values. [object Object] 当存在带有键和值的对象时,会在日志中出现。 You can access properties in an object wth dot notation (.) eg您可以使用点符号 (.) 访问对象中的属性,例如

objectName.propertyName

If properyName is another object it will still return [object Object] and so you need to look for another property within that.如果properyName 是另一个对象,它仍将返回[object Object],因此您需要在其中查找另一个属性。 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.当使用带有 node 的 console.log 并且您有一个深层嵌套的对象时,您可能无法查看嵌套对象的内容。 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.尽管您必须在文件中要求 util。


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 eg using myObject.toString() .问题在于它将 myObject 转换为控制台中的字符串,例如使用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.控制台现在可以解释myObject并很好地显示它。

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 ).我也遇到了这个问题,在 node.js 终端中结合“watchman-make”运行以下代码(watchman-make:请参阅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:以下代码(显示 node.js 输出)说明了在接受的答案/评论中提出的观点:

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}}}}}

Use one of the options:使用以下选项之一:

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

I think JSON.stringify(yourObjName) is the perfect solution.我认为JSON.stringify(yourObjName)是完美的解决方案。 you may apply it in your code segment.您可以在您的代码段中应用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM