Automattic / Automattic/mongoose

can't update document before next request

Open
#5,614 2 comments 0 reactions 0 assignees View on GitHub
plugin
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

hi.

i'm using some code like this:

user.findFlag in initial is 1

```javascript

db.User.find({ _id: 1}, (err, user) => {
if(user.findFlag === 1){

// do some other things

db.User.findOneAndUpdate(
{ _id: 1 },
{ findFlag: 0}, { upsert: true }, (err, user) => {
if (err) return callback(err)

callback(null, user)
});
}
else if(user.findFlag === 0){
db.User.findOneAndUpdate(
{ _id: 1 },
{ findFlag: 1}, { upsert: true }, (err, user) => {
if (err) return callback(err)

callback(null, user)
});
}
})
```

if client request more than one time without delay, user.findFlag doesnt change and just
**if(user.findFlag === 1)** excuted.
i know it is because of async but how to fix it?

nodejs: 8.3.0, express 4.15.4, mongoose 4.11.9

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reported behavior through the db.User.find and db.User.findOneAndUpdate callbacks using the stated Node.js, Express, and Mongoose versions. Check how overlapping client requests observe findFlag, then define and verify the expected result for repeated requests; the payload names no repository files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, mongodb, nodejs
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.