1

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

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

Try to change to assert.equal(3, result.n); , this occurred to me a while ago, I think that's what solved it

Bruno Mello
  • 25
  • 1
  • 8
  • Now I get ```AssertionError [ERR_ASSERTION]: 3 == undefined at C:\Users\Moham\desktop\Projects\FruitsProject\app.js:46:16``` app.js:46:16 is the line that contains 'assert.equal(3, result.n)'. –  Aug 12 '21 at 19:36
  • do this, return your code to what it was before, and print the result value to see how your object is doing. – Bruno Mello Aug 13 '21 at 14:58
  • Yeah I tried that too but the result doesn't appear on the screen... It just gives me the AssertionError. –  Aug 13 '21 at 15:12
0

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.