Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3547

TypeError: Cannot read property 'n' of undefined

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • 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: "Apple" , score: 8, review: "Great fruit" }

      ,

      { name: "Orange" , score: 6, review: "Kinda 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); }

      );
      }

       

       

      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

            Assignee:
            daria.pardue@mongodb.com Daria Pardue
            Reporter:
            saurabhkaran11@gmail.com Saurabh Agrawal
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: