Tejas Hegde
Tejas Hegde

Reputation: 55

Javascript object printing as object object

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

Upvotes: 3

Views: 3035

Answers (3)

jfriend00
jfriend00

Reputation: 707148

[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.

Upvotes: 6

Nayan
Nayan

Reputation: 658

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

Upvotes: 3

Abishek
Abishek

Reputation: 152

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

Upvotes: 1

Related Questions