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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34264800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:06:19  来源:igfitidea点击:

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

javascriptnode.jsfunctioncallback

提问by daniel the man

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] .

我是 Java 脚本和节点 js 的新手,我试图从 MySQL 数据库获取一个值,返回值是 [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);
        });
};

回答by George

[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

[object Object] 当存在带有键和值的对象时,会在日志中出现。您可以使用点符号 (.) 访问对象中的属性,例如

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

如果properyName 是另一个对象,它仍将返回[object Object],因此您需要在其中查找另一个属性。属性还可以包含方法(函数)。例如,如果您想获取对象的字符串版本以比较它们,请使用

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:

当使用带有 node 的 console.log 并且您有一个深层嵌套的对象时,您可能无法查看嵌套对象的内容。在这种情况下,您可以使用:

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 e.g. using myObject.toString(). In this case, you can make it easier for yourself and separate it like this:

这样做的问题是它将 myObject 转换为控制台中的字符串,例如使用myObject.toString(). 在这种情况下,您可以让自己更轻松,并像这样将其分开:

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

And the console can now interpret myObjectand display it nicely.

控制台现在myObject可以很好地解释和显示它。

回答by Victoria Stuart

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

回答by moulay youssef Asakour

Use one of the options:

使用以下选项之一:

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