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