Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ZeroByter opened this issue Aug 27, 2020 · 1 comment
Open

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

ZeroByter opened this issue Aug 27, 2020 · 1 comment

Comments

@ZeroByter
Copy link

ZeroByter commented Aug 27, 2020

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
Copy link
Author

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant