我正在使用Mongoose npm 模块来管理MongoDB。下面是我将要更新的MongoDB集合的架构。
var UserSchema = new Schema({
username: {
type: String,
unique: true,
required: true
},
email: {
type: String,
unique: true,
required: true
},
cards: []
});
module.exports = mongoose.model('User', UserSchema);
在 post 请求中,这里的 req 是 post 请求的请求对象,而 res 则是响应对象。
User.findById(userID).exec(function (err, doc) {
let cardInfo = req.cardInfo
let cardIndex = req.cardIndex
doc["cards"][0] = cardInfo;
console.log(doc)
/* here I got right doc object as I requested
{
"_id": "59f3bdd488f912234fcf06ab",
"email": "test@gmail.com",
"username": "test",
"__v": 2,
"cards": [
{
"testNo": "42424242424242"
}
]
}
*/
doc.save(function (err) {
if (err) {
return res.json({
success: false,
msg: 'Card add error'
});
}
res.json({
success: true,
msg: 'Successful updated card.'
});
});
})
我收到了消息“成功更新卡片”,但实际上它并没有保存。如何解决?谢谢。
cardIndex
在哪里被使用了? - chridam