我对java脚本和节点js非常安静,我想从MySQL DB获取值,并且返回值是[object Object]而不是字符串。 我在网上没有找到任何答案,这是什么问题。 我希望这里有人可以帮忙。 的行值是[object Object]。
这里是我的函数 $ pre> 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); 回调(行); }); };
解决方案
[object object]一个包含键和值的对象。例如
objectName.propertyName如果properyName是另一个对象,它仍然会返回[object Object],因此您需要在其中查找另一个属性。 属性也可以包含方法(函数)。 如果你想获得一个对象的字符串版本以比较它们,那么使用
JSON .stringify(对象名);在节点上使用console.log时,如果您的对象深度嵌套,则可能无法查看嵌套的对象内容。在这种情况下,您可以使用:
console.log(util.inspect(objectName,false,null));查看整个对象。尽管您必须在文件中需要util。
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); }); };解决方案
[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.propertyNameIf 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.
更多推荐
节点js函数返回[object Object]而不是字符串值
发布评论