I am having trouble with mongoosejs, I think. I am trying to keep an array of objects at a specific size of 2. When this function is called it adds an item to the array and slims it down if necessary. However when I save the array the size is not trimmed down to 2. Follow the code and comments. Thanks for any help you can provide.
user.location.push(req.body); //Add a new object to the array.
if(user.location.length > 2) //If the array is larger than 2
user.location.splice(0,1); //Remove the first item
console.log(user.location); //This outputs exactly what I would expect.
user.save(function(err, updatedUser){
if(err)
next(new Error('Could not save the updated user.'));
else {
res.send(updatedUser); //This outputs the array as if it was never spliced with a size greater than 2.
}
});
locationdefined in your schema?