These are chat archives for Automattic/mongoose

9th
Aug 2016
Dávid Kőszeghy
@dejwoo
Aug 09 2016 19:28
hey guys, anybody know how to solve RangeError: Maximum call stack size exceeded? I am trying to bulk save big number of documents, by first creating plain js objects which match schema model and then calling Model.collection.inser(arrayOfObjects, {}, callback) where even when array has 5 objects I get RangeError
Eric Grosse
@ericgrosse
Aug 09 2016 19:58
When you use a schema post hook, is there a way to compare the old document state to the new one?
In the docs it shows
schema.post('save', function(doc) {
  console.log('%s has been saved', doc._id);
});
But that only gives you the updated doc state
I'm trying to setup cascade behavior where changing a user's userID updates all the userIDs for his associated blogs and posts
I've been using handlers such as the following
  put: function(req, res, Model, userId) {
    var Models = [Model.User, Model.Blog, Model.Post];

    // Only cascade for changes to the user's username
    if (req.body.username) {
      Promise.all(Models.map(function(model, index) {
        // If Model.User, update the username of just that user.
        // Else if Model.Blog or Model.Post, update the userId of all the blogs and posts pertaining to that user.
        if (index === 0) {
          return model.findOneAndUpdate({username: userId}, {username: req.body.username});
        }
        else {
          return model.update({userId: userId}, {userId: req.body.username}, {multi: true});
        }
      }))
      .then(function() {
        res.status(204).end();
      })
      .catch(function(err) {
        res.status(500).send(err);
      });
    }
    else {
      routeHandler.put(req, res, Model.User, {username: userId});
    }  
  }
But then I have to write a handler like this for every PUT request I make, which is why doing the cascade through the post hook would be so much more convenient (and less error prone)
shaark
@antoine2vey
Aug 09 2016 21:54
Hi guys, looking for some help, I can't return my value when Im doing a find query in my database... Here is my code
function getBankAmount(user) {
    ladderboardPlayer.findOne({name: user}).exec(function(err, doc) {
        if(doc){
            console.log(doc.bank) //returns 10000
            return doc.bank; //returns undefined
        }
    });
}
Cristyan Sepúlveda V
@cristyansv
Aug 09 2016 22:00
Hello, anyone knows a good article about data modeling?
Daniel Meng
@danielmeng07_twitter
Aug 09 2016 22:34
@antoine2vey query looks okay to me. Have you checked it against the raw data? The other possibility is if Mongoose wasn't able to validate this scheme field for some reason