我想mongoosejs有问题。我试图保留一个特定大小为2的对象数组。当调用这个函数时,它会将一个项目添加到数组中,并在必要时将其缩小。但是,当我保存数组时,大小不会缩减为2.请遵循代码和注释。感谢您的任何帮助,您可以提供。MongooseJS不能正确保存数组
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.
}
});
架构中的如何界定'location'? – JohnnyHK
就像一个数组“位置:[]” – Scott