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

Check if id exists then either update or create #10

Open
polluterofminds opened this issue Nov 19, 2018 · 1 comment
Open

Check if id exists then either update or create #10

polluterofminds opened this issue Nov 19, 2018 · 1 comment

Comments

@polluterofminds
Copy link

I am sure this has to be possible but I have not been able to make heads or tails of how to do it. I would like to check if an id already exists in the db, and if it does, I'd like to update it. Otherwise, I'd like to create a new item in the db with the given id.

I tried this:

if(mydb.read(idToCheck)) {
  mydb.update(idToCheck, { data: dataToPost })
} else {
  mydb.create({ _id: idToCheck, data: dataToPost })
}

But that returns true every time even if the id does not exist in the db. Any tips?

@ngohoaiphuong
Copy link

mydb.read(idToCheck).then(
(response) => {
response === undefined ? mydb.create({ _id: idToCheck, data: dataToPost }) : mydb.update(idToCheck, { data: dataToPost })
}
)

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

2 participants