admin管理员组文章数量:1357707
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
3 Answers
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));
})
本文标签: nodejsJavascript object printing as object objectStack Overflow
版权声明:本文标题:node.js - Javascript object printing as object object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744045727a2581484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论