assert.equal(3, result.result.n); TypeError: Cannot read property 'n' of undefined

Viewed 1064

i am getting an error which is saying that n is not defiend at line 47 and i have used all the method to solve it but i am unable to solve it. it is showing and error which is n is undefiend i want to why it is giving me an error and what is the real mistake i am doing to solve it

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'fruitDB';

// 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: "apple",
            score:5,
            review: "very very nice"
        },
        { 
            name: "orange",
            score: 7,
            review: "very very sour"
        },
        { 
            name: "Banana",
            score: 5,
            review: "best fruit"
        }
    ], 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);
    });
  }

please solve it

4 Answers

Since the result object is changed, it no longer contains the result or the ops field

function (err, result)
{
    assert.equal(err, null);
    assert.equal(3, result.insertedCount);
    assert.equal(3, Object.keys(result.insertedIds).length);
    console.log("Inserted 3 documents into the collection");
    callback(result);
});

use these assert statements, these should work fine for your case.

The issue is that your "result.result" is undefined - your "result" has no defined ".result" property. Try to log or debug the root result object, to see if it has anything at all.

I might be misreading, but it seems that mongoDB's InsertMany does not return an object for you, it is only able to throw expections on failure. https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#behaviors

Only the insert single returns a WriteResult object: https://docs.mongodb.com/manual/reference/method/db.collection.insert/#writeresult

If you're going through Angela Yu's The Complete 2021 Web Development Bootcamp then you should complete the code (as shown below) to get rid of the error.

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');


const url= 'mongodb://localhost:27017';

const dbName = 'fruitDB';

const client = new MongoClient(url);


client.connect(function(err){
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  findDocuments(db, function(){
    client.close();
  });
});

const insertDocuments = function(db, callback) {
  const collection = db.collection('fruits');

  collection.insertMany([
    {
      name: "Apple",
      score: 8,
      review: "Great Fruit"
    },
    {
      name: "Orange",
      score: 6,
      review: "Sweet & Sour"
    },
    {
      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);
  });
};

const findDocuments = function(db, callback){
  
  const collection = db.collection('fruits');

  collection.find({}).toArray(function(err, fruits) {
    assert.equal(err, null);
    console.log("Found the following records");
    console.log("fruits");
    console.log(fruits);
  });
}

hence mongodb is frequiently being updated and its functions are being changed.

change

assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);

with

assert.equal(3, result.insertedCount);
assert.equal(3, Object.keys(result.insertedIds).length);
Related