'When working with the native mongoDB driver, Node.js and hyper terminal, I receive an error saying "TypeError: Cannot read property 'n' of undefined"
I am using vs-code, hyper-terminal (Git Bash), JavaScript, Node.js, Express, MongoDB and its native driver but while I finished up writing my code, I wrote node app.js
(I am using app.js as my javascript file for this project) to see if my code works but it gave me an error saying:
TypeError: Cannot read property 'name' of undefined
And that 'n' was in line 46 which I basically went into and found assert.equal(3, result.result.n);
. I don't see what's wrong with this. My full code is below.
My app.js:
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, { useNewUrlParser: true });
// Use connect method to connect to the server
client.connect(function(err) {
assert.equal(null, err);
console.log("Successfully Connected to the 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: "Apple",
score: 8,
review: "Great fruit but I didn't really like the wax"
},
{
name: "Orange",
score: 6,
review: "Great for sour-candy-eaters like me!"
}, {
name: "Banana",
score: 10,
review: "Very nice! Gave me alot of energy!"
}
], 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);
});
}
The error at hyper terminal
TypeError: Cannot read property 'name' of undefined
at C:\Users\*****\desktop\Projects\FruitsProject\app.js:46:39
at C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\utils.js:530:9
at C:\Users\Moham\*****\Projects\FruitsProject\node_modules\mongodb\lib\operations\execute_operation.js:49:55
at C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\utils.js:530:9
at completeEndSession (C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\sessions.js:147:17)
at C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\sessions.js:157:13
at Object.maybePromise (C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\utils.js:516:5)
at ClientSession.endSession (C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\sessions.js:133:
24)
at C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\operations\execute_operation.js:49:36
at C:\Users\*****\desktop\Projects\FruitsProject\node_modules\mongodb\lib\operations\insert.js:79:13
PS: I am following a Udemy full-stack web development course and for my instructor it worked right, but for me, it just doesn't work. Please tell me if you spot any mistake.
Edit: I just want it to say "successfully connected to the server" and "inserted 3 documents into the collection".
Solution 1:[1]
Try to change to assert.equal(3, result.n);
, this occurred to me a while ago, I think that's what solved it
Solution 2:[2]
I know this is late, but if you console.log(result);
,
You'll see that the result object has no attribute called ops or result.
, Remove assert.equal(3, result.result.n);
and assert.equal(3, result.ops.length);
, and Instead add assert.equal(3, result.insertedCount);
.
I had the same issue and that solved it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Eleazar Udo |