我正在使用mongoDB填充模型,当我尝试将数据保存到架构中时会引发错误:
message: 'Cast to ObjectId failed for value
这是我的模式
jobSiteInformation: {
buildingName: String,
street: String,
city: String,
},
data: [{
ref: 'data',
type: Schema.Types.ObjectId,
required: true,
}],
阶段架构是这样的
const listSchema= new Schema({
name: String,
progress: Number,
list: [],
});
此阶段模式是阶段内部的数组,这是相当大的,这就是我移入模型的原因。
无论如何,这是我的路线,当我运行此路线时,会抛出我上面粘贴的错误。
router.post('/', async (req, res, next) => {
const info= new List({
jobSiteInformation: req.body.jobSiteInformation,
});
try {
const install = req.body.list;
install.map((inst) => info.lists.push(inst));
const saved= await partial.save();
return res.status(201).json({
result: saved,
});
} catch (e) {
console.log(e);
return next(e);
}
});
我尝试过Google,但是找不到我想要的东西。我也阅读了其他帖子,但在这里看不懂我在做什么错。
答案 0 :(得分:1)
假设相模式的猫鼬模型为Phase
// import Phase from ../models/phase
router.post('/request/partial', async (req, res, next) => {
const partial = new PartialRequest({
jobSiteInformation: req.body.jobSiteInformation,
});
try {
const install = req.body.installations;
let savedPhases = await Phase.insertMany(install); // TODO: handle error case
savedPhases.map((inst) => partial.installations.push(inst["_id"]));
const savedPartials = await partial.save();
console.log(savedPartials);
return res.status(201).json({
result: savedPartials,
});
} catch (e) {
console.log(e);
return next(e);
}
});