These are chat archives for Automattic/mongoose

30th
Aug 2018
mirik999
@mirik999
Aug 30 2018 11:04
@lineus Hi again , with findOneAndUpdate we can use { new: true } , but how can i use new: true with findOne or find method ?
Kev
@lineus
Aug 30 2018 11:06
@mirik999 find/findOne always return the value of the document in the collection when the query is executed, unlike findOneAndUpdate which by default returns the previous value of the document before the update.
mirik999
@mirik999
Aug 30 2018 11:07
but i get previous value (( after refreshing i get latest value.
Kev
@lineus
Aug 30 2018 11:10
@mirik999 do you mean in your browser?
mirik999
@mirik999
Aug 30 2018 11:14
@lineus in console. doesnt matter back end or front end. after updating findOne return previous value
Kev
@lineus
Aug 30 2018 11:15
are you sure that the findOne is actually running after the update?
mirik999
@mirik999
Aug 30 2018 13:03
    socket.on('editedPlayer', async data => {

      await Player.findOneAndUpdate({ _id: data.editedPlayer._id }, {}, {new: true}, (err, player) => {
        player.silver.map((pl, idx) => {
          if (pl.seller == data.seller) {
            pl.set('count', data.newCount)
            player.save();
          }
        })
      })

      Player.find({})
      .populate('author', 'nick color guild alliance')
      .populate('silver.seller', 'nick color guild alliance')
      .exec((err, players) => {
        io.sockets.emit('getAllPlayers', players)
      });

    })
Kev
@lineus
Aug 30 2018 13:27
@mirik999 in your listener, you are using both the promise and the callback simultaneously. This gist demonstrates that doing so results in multiple calls to findOneAndUpdate. Basically, the findOneAndUpdate() promise returns and the find() executes before the save() in the findOneAndUpdate() callback executes.
Kev
@lineus
Aug 30 2018 13:34
This should probably work:
socket.on('editedPlayer', async data => {
  let player =  await Player.findOne({ _id: data.editedPlayer._id });
  player.silver.map((pl, idx) => {
    if (pl.seller == data.seller) {
      pl.set('count', data.newCount);
    }
  });

  await player.save();

  let players = await Player.find({})
    .populate('author', 'nick color guild alliance')
    .populate('silver.seller', 'nick color guild alliance');

  io.sockets.emit('getAllPlayers', players);

});
Kev
@lineus
Aug 30 2018 17:21
the Node.js security group has found a security issue in Mongoose, severity 7/10. It only impacts you if you use strict: false. We strongly recommend upgrading to Mongoose ^5.2.12 (or ^4.13.17 if you're still on 4.x) as soon as possible, especially if you disable strict mode in any of your schemas.
We'll post a detailed report on the security vulnerability in a few weeks time so you have time to upgrade before we publicly disclose it.
Note: I edited the version numbers in the previous message
quorak
@quorak
Aug 30 2018 20:12
hey guys. thx for this beautiful piece of work. I just wrote up https://github.com/quorak/mongoose-vault , a small wrapper to use hashicorp's encryption-as-a-service . Feedback is much appreciated.
hillct
@hillct
Aug 30 2018 20:12
Good afternoon all. Assuming I have a document ‘compositions’ with a subdocument array ‘movements’, is this a legitimate mechanism for removing an item from that array? composition.movements.id(req.params.movementId).remove(); I’m getting the error that remove() is not a method of undefined, so in narrowing the issue down, do I need to cast my string representation of a document ID, into a document ID object before passing it to the .id() method? Can I not chain the remove() method in this way? Docs are unclear on point 1, and I resume chaining is allowed, based on examples in the manual...