These are chat archives for Automattic/mongoose

27th
Apr 2018
Ghost
@ghost~5928d90bd73408ce4f629b9e
Apr 27 2018 12:47

Hello all,

Should i always use exec? I know the difference between exec and a normal callback, but i see a lot of people using exec even they're not dealing with queries.

Kev
@lineus
Apr 27 2018 13:02
@2HexGFX_twitter can you give an example where you're not sure if it's necessary?
Ghost
@ghost~5928d90bd73408ce4f629b9e
Apr 27 2018 13:11
Like this
 Account.update({_id: req.body.user_id}, {$set: {display_email:req.body.entry}})
            .exec((err, user) => {
                if(err){
                    throw err;
                }


                if(user){
                    res.send(user)
                }
            })
Kev
@lineus
Apr 27 2018 13:14
Model.update() returns a query, calling exec in this case is probably a personal choice to shorten the line length.
Ghost
@ghost~5928d90bd73408ce4f629b9e
Apr 27 2018 13:15
@lineus right i was actually using findByIdAndUpdate
What the case where exec is not the choice?
Kev
@lineus
Apr 27 2018 13:26
if you're using query builder methods and callbacks, .exec() is necessary.
If you're not using query builder methods but you are using callbacks,.exec() is an aesthetic choice.
if you're using promises .exec() is unnecessary ( unless you want to run the query now and postpone calling then until later )
Kev
@lineus
Apr 27 2018 13:36
@2HexGFX_twitter ^
Ghost
@ghost~5928d90bd73408ce4f629b9e
Apr 27 2018 13:41
@lineus Thanks all clear to me now :smile:
Kev
@lineus
Apr 27 2018 13:50
@2HexGFX_twitter it's a good question. I had to think about the promise part a bit, if you run this gist, you can see what I meant by executing the query now vs later.
brian-petersen
@brian-petersen
Apr 27 2018 18:09

So I'm trying to call .findOneAndUpdate with like so

await CardMeta.findOneAndUpdate({ cardXmlId: card.xmlId }, {
  summary: 'sum',
   directorInstructions: 'hi',
})

But online the summary field is updated. I checked the schema and it has the other field. Is there something I'm doing that's clearly wrong? The only difference I can tell is that the directorInstructions is camel-case.

Kev
@lineus
Apr 27 2018 19:30
@brian-petersen you still loooking for some help?