1

i have a document which has subdocument array in it(size is 6), then i remove 5 element at the same time, it perfectly updates array, but cant find the document after updated, here the code to update it

await gameRoom.findOneAndUpdate({ 'players._id': currentUser._id }, { $pull: { players: { _id: currentUser._id } } }, { 'new': true },
             (err, docc) => {
                 console.log('currentUser id ' + currentUser._id + ' time ' + new Date().toUTCString());
                 console.log('doc ' + JSON.stringify(docc));
                 if (err) console.log('error at logging out gameRoom.findOne ', '\n ' + err);
                 else if (docc == null) console.log('how it can be null');


        });

and this is result

currentUser id 5eb285720105534970994c92 time Wed, 06 May 2020 09:38:26 GMT
doc null
how it can be null
currentUser id 5eb2856a0105534970994c89 time Wed, 06 May 2020 09:38:26 GMT
doc null
how it can be null
currentUser id 5eb2856f0105534970994c8f time Wed, 06 May 2020 09:38:26 GMT
doc IsNotNull

currentUser id 5eb2856d0105534970994c8c time Wed, 06 May 2020 09:38:26 GMT
doc null
how it can be null
currentUser id 5eb285750105534970994c96 time Wed, 06 May 2020 09:38:26 GMT
doc IsNotNull

currentUser id 5eb285680105534970994c85 time Wed, 06 May 2020 09:38:30 GMT
doc IsNotNull


so firstly i pull 5 element from array , then i pull last one as you can see in result, all of them removed from array perfecly with update, but someof them cant reach the docc they updated how is this possible ?

Jsennin
  • 153
  • 2
  • 12
  • Please post a sample document. – prasad_ May 06 '20 at 10:03
  • What is `await` doing with your callback syntax. See [ES6 sytax](https://mongodb.github.io/node-mongodb-native/3.6/reference/ecmascriptnext/crud/) and [callback syntax](https://mongodb.github.io/node-mongodb-native/3.6/tutorials/crud/). – prasad_ May 06 '20 at 10:10
  • @prasad_ okay i deleted unnecessart parts in code, so it is easier to read. are you talking about await gameRoom. ? because i cant see await in callback – Jsennin May 07 '20 at 17:55
  • 1
    I think this may be an issue with your code: [async await not working with callback node (without promise)](https://stackoverflow.com/questions/53707410/async-await-not-working-with-callback-node-without-promise). – prasad_ May 08 '20 at 01:17

1 Answers1

1

after changing { 'new': true } to { new: true } it fixed,also if upsert:true used, looks like it also fixes as well.like {upsert:true, new: true }

Jsennin
  • 153
  • 2
  • 12