4

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);
    })
};
1
  • Did you try JSON.stringify on your result object? Commented Apr 25, 2020 at 20:10

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

@Nayan..yes that was the reason..ty for the help
1

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

2 Comments

@Abhishek..Yes it helps..thanks..but i am not able to understand why this is happening.Even when i am getting the result object in operations.js then i am able to print that Json object.
had the code been console.log(result), you would have seen the data. if you concatenate with a string, then you need to use JSON.stringify. Or, as @nayan mentioned in the other answer, you can do like console.log("Found Documents: \n", result)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.