Ex5: Insert. / Keep getting "AssertionError"
See original GitHub issueEx 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:
- Created 7 years ago
- Reactions:1
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had the same problem. The issue was that the callback wasn’t returning the object I thought.
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 :