【发布时间】:2018-10-25 14:22:00
【问题描述】:
我正在开发一个餐桌规划器应用程序,可以将客人分配到餐桌。表模型具有以下 Schema:
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const tableSchema = new mongoose.Schema({
name: {
type: String,
required: 'Please provide the name of the table',
trim: true,
},
capacity: {
type: Number,
required: 'Please provide the capacity of the table',
},
guests: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Guest',
}],
});
module.exports = mongoose.model('Table', tableSchema);
客人可以在应用程序中(使用 React DND)拖放到“表格”React 组件中。在被放到表上后,会向 Node.js 方法发出 Axios POST 请求以更新数据库并将访客的对象 ID 添加到表模型中的数组中:
exports.updateTableGuests = async (req, res) => {
console.log(req.body.guestId);
await Table.findOneAndUpdate(
{ name: req.body.tablename },
{ $push: { guests: req.body.guestId } },
{ safe: true, upsert: true },
(err) => {
if (err) {
console.log(err);
} else {
// do stuff
}
},
);
res.send('back');
};
这按预期工作,除了每个删除的来宾,表模型的来宾数组使用相同的来宾对象 ID 更新两次?有谁知道为什么会这样?
我已尝试记录 req.body.guestID 以确保它是单个值,并检查此函数是否被调用两次。但这些测试都没有带来意想不到的结果。因此我怀疑我的findOneAndUpdate 查询有问题?
【问题讨论】:
-
一旦出现问题,您是否有 Table JSON 对象的示例?我想看看“ObjectIDs to array two”的 JSON 代表,希望能了解 mongoose 的行为方式。
标签: mongodb mongoose mongodb-query