节点js函数返回[object Object]而不是字符串值 [英] node js function return [object Object] instead of a string value

查看:241
本文介绍了节点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]而不是字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆