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

编程入门 行业动态 更新时间:2024-10-28 08:26:00
本文介绍了节点js函数返回[object Object]而不是字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对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.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.

更多推荐

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

本文发布于:2023-11-05 19:51:22,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   函数   而不是   字符串值   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!