【问题标题】:Javascript object printing as object objectJavascript对象打印为对象对象
【发布时间】:2020-04-25 20:07:45
【问题描述】:

我对 node 和 mongo db 都很陌生。我正在创建从 node 到 Mongo 的连接并尝试 CRUD 操作。我的操作是在 operations.js 中定义的,我正在从索引调用函数。

我面临的问题是当我打印回调参数时

coll.find({}).toarray() - 结果是我得到了想要的输出

[
  {
    _id: 5ea4843b0f28320524d23f14,
    name: 'Vadonut',
    description: 'Test Vadonut'
  },
]

但是当我从 index.js 打印结果时,这是 operation.js 中函数回调的结果,我得到的输出为

[object Object]

我可以得到帮助吗????

index.js:

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const dboper = require('./operations')
const url = "mongodb://localhost:27017/";
const dbname = "dishes";

MongoClient.connect(url,(err,client)=>{
    assert.equal(err,null);

    console.log("Connected correctly correct to the server");

    const db =client.db(dbname);


    dboper.insertdocument(db,{"name":"Vadonut","description":"Test Vadonut"},'dishes',(result)=>{
        console.log('Insert Document:\n'+result);

        dboper.finddocument(db,'dishes',(result)=>{
           console.log("Found Documents :\n"+result);
    })

})  

****操作.js****

const assert = require('assert');

exports.insertdocument = (db,document,collection,callback)=>{
    const coll = db.collection(collection);
    coll.insertOne(document,(err,result)=>{
        assert.equal(err,null);
        console.log("Inserted " + result.result.n + "documents inserted into the collection"+collection);
        console.log(result.ops);
        callback(result);

    })

};

exports.finddocument = (db,collection,callback)=>{
    const coll = db.collection(collection);
    coll.find({}).toArray((err,docs)=>{
        assert.equal(err,null);
        console.log(docs);
        callback(docs);
    })
};

【问题讨论】:

  • 您是否在结果对象上尝试过 JSON.stringify?

标签: javascript node.js


【解决方案1】:

[object Object] 是对象的默认/自动字符串转换。

因此,如果您在字符串操作表达式中的任何位置使用对象,例如:

let x = {greeting: "hello"};
let str = "I would like to say the greeting " + x;
console.log(str);

然后,JS 解释器将尝试将您的对象 x 转换为字符串,默认字符串转换为 [object Object],因此您将得到以下结果:

 I would like to say the greeting [object Object]

您需要做的是避免在字符串表达式中的任何位置使用 Javascript 对象,或者在将对象加入字符串表达式之前使用 JSON.stringify() 将对象显式转换为 JSON。

我会替换这个:

console.log("Inserted " + result.result.n + "documents inserted into the collection"+collection);

用这个:

console.log("Inserted ",  result.result.n, "documents inserted into the collection", collection);

然后,您将整个对象传递给console.log(),它将在它们上显示正常的对象,而不是让 JS 尝试将它们自动转换为字符串。

您也可以使用JSON.stringify(result.result.n) 手动将这些对象转换为字符串形式,然后在字符串表达式中使用它们。

【讨论】:

    【解决方案2】:

    你可以试试 JSON.stringify(result)

    喜欢这个

    dboper.insertdocument(db,{"name":"Vadonut","description":"Test Vadonut"},'dishes',(result)=>{
            console.log('Insert Document:\n'+JSON.stringify(result));
    
            dboper.finddocument(db,'dishes',(result)=>{
               console.log("Found Documents :\n"+JSON.stringify(result));
        })
    

    【讨论】:

    • @Abhishek ..是的,它有帮助..谢谢..但我无法理解为什么会这样。即使我在 operations.js 中获取结果对象,我也可以打印那个 Json 对象。
    • 如果代码是console.log(result),您就会看到数据。如果你用一个字符串连接,那么你需要使用 JSON.stringify。或者,正如@nayan 在另一个答案中提到的那样,您可以像 console.log("Found Documents: \n", result)
    【解决方案3】:

    这是因为控制台日志带有“,”和“+”的两种不同行为。

    let user = {"_id":"5ea4843b0f28320524d23f14", "name":"Vadonut"};
      
    console.log("doc"+ user)
    
    console.log("doc", user)

    还有支票this

    【讨论】:

    • @Nayan..是的,这就是帮助的原因..ty
    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多