最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Javascript object printing as object object - Stack Overflow

programmeradmin1浏览0评论

I am very new to both node and mongo db.I was creating a connection from node to Mongo and trying the CRUD Operations.My operations are defined in operations.js and i am calling the functions from index.

The issue i am facing is when i am printing the callback parameter from

coll.find({}).toarray() - that is result i am getting the desired output as

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

but when i am printing the result from index.js which is a result of the callback from the functions in operation.js i am getting the output as

[object Object]

Can i get help on this?????

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

})  

****operations.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);
    })
};

I am very new to both node and mongo db.I was creating a connection from node to Mongo and trying the CRUD Operations.My operations are defined in operations.js and i am calling the functions from index.

The issue i am facing is when i am printing the callback parameter from

coll.find({}).toarray() - that is result i am getting the desired output as

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

but when i am printing the result from index.js which is a result of the callback from the functions in operation.js i am getting the output as

[object Object]

Can i get help on this?????

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

})  

****operations.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);
    })
};
Share Improve this question edited Apr 25, 2020 at 21:15 whoami - fakeFaceTrueSoul 17.9k6 gold badges35 silver badges49 bronze badges asked Apr 25, 2020 at 20:07 Tejas HegdeTejas Hegde 551 silver badge5 bronze badges 1
  • Did you try JSON.stringify on your result object? – alimcharaniya Commented Apr 25, 2020 at 20:10
Add a ment  | 

3 Answers 3

Reset to default 6

[object Object] is the default/automatic string conversion for an object.

So, if you use an object anywhere in a string manipulation expression such as this:

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

Then, the JS interpreter will try to convert your object x to a string and that default string conversion will be [object Object] so you will get a result of:

 I would like to say the greeting [object Object]

What you need to do is either avoid using a Javascript object anywhere in a string expression or explicitly convert the object to JSON with JSON.stringify() before involving it in a string expression.

I would replace this:

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

with this:

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

Then, you're passing whole objects to console.log() and it will do its normal object display on them rather than let JS try to auto-convert them to a string.

You could also manually convert those objects to string form with JSON.stringify(result.result.n) and then use them in string expressions.

It is because of two different behavior of console log with "," and "+".

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

console.log("doc", user)

Also the check this

Can you try with JSON.stringify(result)

like this

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));
    })
发布评论

评论列表(0)

  1. 暂无评论