question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ex5: Insert. / Keep getting "AssertionError"

See original GitHub issue

Ex 5 keeps failing with that error:

{ [AssertionError: { n: 1, ok: 1 } deepEqual { firstName: 'Jane', lastName: 'George' }]
  name: 'AssertionError',
  actual: { n: 1, ok: 1 },
  expected: { firstName: 'Jane', lastName: 'George' },
  operator: 'deepEqual',
  message: '{ n: 1, ok: 1 } deepEqual { firstName: \'Jane\', lastName: \'George\' }',
  generatedMessage: true }
✗ { n: 1, ok: 1 } deepEqual { firstName: 'Jane', lastName: 'George' }

My code:

var mongo = require('mongodb').MongoClient;

mongo.connect('mongodb://localhost:27017/learnyoumongo', function(err, db) {
    if(err) throw err;
    var collection = db.collection('docs');

    collection.insertOne({firstName: process.argv[2], lastName: process.argv[3]}, function(err, data) {
      if(err) throw err;

      data = JSON.stringify(data);
      console.log(data);
      db.close();
    });

});

Is it a bug or am I doing something wrong??

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

14reactions
mdavidmortoncommented, Jun 20, 2016

I had the same problem. The issue was that the callback wasn’t returning the object I thought.

var doc = { firstName: process.argv[2], lastName: process.argv }

// ...
collection.insert(doc, function(err, data) {
    // error handling
    console.log(JSON.stringify(doc)); // <~ doc, not data
    db.close();
}

2reactions
sakthi17commented, Jan 2, 2018

Seems the object returned by the insertOne callback has changed in version 3.x.

It gives an object containing various properties . Refer http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#~insertOneWriteOpCallback

ops is the prop we are looking for. ops is an array of inserted doc objects. console.log( JSON.stringify( result.ops)) where result is the object returned from insertOne() callback.

Still the result wont match. Because of the extra _id property in our ops object. It gets added to every doc inserted by default . We can turn off this behaviour by setting forceServerObjectId == false in .insert() options collection.insertOne(doc , {forceServerObjectId : true}, callback )

There is one more thing to it. We do a stringify on ops which is an array containing inserted objects. But for the assert to work, we need the stringified version of the doc object inserted and not the array containing all the inserted docs. So get ops[0] which is the doc and then JSON-stringify it

Below is the final code where the solution gets passed :

var MongoClient = require('mongodb').MongoClient

var doc = {
        firstName : process.argv[2],
        lastName : process.argv[3]
}
var url = "mongodb://localhost:27017";
MongoClient.connect(url, function(err,client){
        if(err) throw err;
        var db = client.db("learnyoumongo");
        var collection = db.collection("docs");
        collection.insertOne(doc , {forceServerObjectId : true},function(err,result){
                if(err) throw err
                console.log(JSON.stringify(result.ops[0]));
        });
        client.close();
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting an AssertionError when I use the multiprocessing ...
The code to start the multiprocessing must never be "top level". It must only be run if this is the main thread.
Read more >
Unknown Assertion Error · Issue #236 · openai/jukebox - GitHub
I'm running the Jukebox AI code in a Google Collab Pro version. It keeps crashing as an 'Assertion error' at the start of...
Read more >
Python | Assertion Error - GeeksforGeeks
Assertion Error Assertion is a programming concept used while writing a code where the user declares a condition to be true using assert ......
Read more >
Help w/ MakeFeatureLayer and Assertion Error
You are attempting to add the addLayer variable which is a list of layers. And you are iterating over a single layer of...
Read more >
arcpy - Assertion Error, Trying to add .tif to group layer
I keep on receiving this error. Traceback (most recent call last): File "C:\Users\sorrell\AppData\Local\ESRI\Desktop10.2\AssemblyCache{DE572FA2- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found