Open bbosiljcic opened 4 years ago
That sounds like a JS object reference problem. The JSON.parse/stringify explicitly creates a new object, rather than a reference to the old one as is the norm. Object.assign({}, originalDoc) should work too, and it looks a bit better IMHO.
Part of your problem is possibly due to the param names for your nested db.find()
calls. The second one uses the same param names in the same scope (err, docs)
, so that is going to cause some funky problems. I would be interested to see if the problem persists after changing those param names.
We just found an issue where altering an already updated document changes that document in the database.
Steps to reproduce
$set
I created a repo that illustrates the issue https://github.com/bbosiljcic/nedb-set-issue
A snipped from that repo
You can see that
originalDoc.object
has thec
key that was added after the update in the secondconsole.log
.The issue only occurs if
$set
is used and only nested objects are affected.This only happens inMemory, when restarted the results are correced.
We currently fixed the issue by
JSON.parse(JSON.stringify(originalDoc))