我有一个Mongoose模式,如下所示:
const gameSchema = new Schema({
matchNumber: {
type: Number,
required: [true, 'A match must have a number!'],
unique: true
},
red: {
s1: {
type: ObjectId,
ref: 'Match'
},
s2: {
type: ObjectId,
ref: 'Match'
},
s3: {
type: ObjectId,
ref: 'Match'
}
}
});我正在尝试通过Express更新文档中的匹配项。在向:matchNumber/:alliance/:seed/:teamNumber/match发出POST请求时,我执行以下操作:
import * as flatten from 'flat';
let match = req.body;
const game = await Game.findOneAndUpdate(
{ matchNumber },
flatten({ [alliance]: { [seed]: match._id } }),
{ new: true }
);当我发出POST请求时,我得到以下错误:
CastError: Cast to ObjectId failed for value "ObjectID" at path "red.s1"我应该提到我正在使用TypeScirpt,这是以前的半工作,但我遇到了提到的here的问题,它的解决方案导致了我现在正在经历的事情。
发布于 2019-12-30 04:00:47
通过移除平面包并将其更改为以下内容,成功修复了该问题:
const loc = `${alliance}.${seed}`;
const game = await Game.findOneAndUpdate(
{ matchNumber },
{ $set: { [loc]: match._id } },
{ new: true }
);https://stackoverflow.com/questions/59521870
复制相似问题