nedb icon indicating copy to clipboard operation
nedb copied to clipboard

Using $unset in update sets the target to 'null'

Open ZeroByter opened this issue 4 years ago • 1 comments

I found this strange sort of behavior/bug in my Electron + ReactJS app.

When I delete an item from the database using db.update(..., {$unset: ...}), everything works fine during that same session. But when I close the app and open it up again, the object I deleted is replaced by 'null' and causes crashes in my app

Yes, I could null-check and if so skip that null entry, but in a big database with lots of null entries that would introduce inefficiencies and I really shouldn't have to null-check where I know I deleted an entry.

I suspect this problem arises from the database compaction during the startup of the second session.

My $unset code looks like this.

Is this intentional? Is there a way to prevent this? When I unset an object I expect it to be removed from the array, not be replaced by null.

ZeroByter avatar Aug 27 '20 08:08 ZeroByter

I found a work-around for this. In the props, I give my component the whole database object, then when I need to delete something, instead of:

var updateData = {}
updateData.$unset = {}
updateData.$unset["lists." + this.props.listIndex] = true
db.update({_id: this.props.boardId}, updateData, function(){
	this.props.reloadBoard()
}.bind(this))

I do:

var deletedList = this.props.board.lists
deletedList.splice(this.listIndex)

var updateData = {}
updateData.$set = {}
updateData.$set["lists"] = deletedList
db.update({_id: this.props.board._id}, updateData, function(){
	this.props.reloadBoard()
}.bind(this))

ZeroByter avatar Aug 29 '20 13:08 ZeroByter