-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
Empty show more show less
-
Not Needed
Here is my code:
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'fruitsDB';
// Create a new MongoClient
const client = new MongoClient(url);
// Use connect method to connect to the Server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
insertDocuments(db, function()
{ client.close(); });
});
const insertDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('fruits');
// Insert some documents
collection.insertMany([
,
,
{ name: "Banana" , score: 9, review: "Great Stuff!" }], function(err, result)
{ assert.equal(err, null); assert.equal(3, result.result.n); assert.equal(3, result.ops.length); console.log("Inserted 3 documents into the collection"); callback(result); });
}
Here is the error:
assert.equal(3, result.result.n);
^
TypeError: Cannot read property 'n' of undefined
at /Users/saurabhagrawal/Desktop/FruitsProject/app.js:47:35
at /Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:530:9
at /Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/operations/execute_operation.js:49:55
at /Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:530:9
at completeEndSession (/Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/sessions.js:147:17)
at /Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/sessions.js:157:13
at Object.maybePromise (/Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:516:5)
at ClientSession.endSession (/Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/sessions.js:133:24)
at /Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/operations/execute_operation.js:49:36
at /Users/saurabhagrawal/Desktop/FruitsProject/node_modules/mongodb/lib/operations/insert.js:79:13