gpt4 book ai didi

node.js - Mongoose:为值 "[ 5ac5cfb41fca8a22f519cb22 ]"转换为数组失败

转载 作者:搜寻专家 更新时间:2023-10-31 23:19:43 25 4
gpt4 key购买 nike

我正在尝试在现有游戏中插入一个回合,但出现以下错误:

Game validation failed: rounds.1.questions: Cast to Array failed for value "[ 5ac5cfb41fca8a22f519cb22 ]" at path "questions"

模式的:

const roundSchema = Schema({
roundNumber: {
type: Number,
required: true,
},
categories: {
type: [String],
required: true
},
questions: {
type: [Schema.Types.ObjectID],
ref: 'Question',
required: true,
}
});

const gameSchema = Schema({
code: {
type: String,
required: true,
},
teams: {
type: [Schema.Types.ObjectID],
required: false,
},
rounds: [roundSchema]
});

const questionSchema = Schema({
question: {
type: String,
required: true,
},
answer: {
type: String,
required: true,
},
category: {
type: String,
required: true,
}
});

插入函数:

function createRoundForGame(game, round) {
round.questions = round.questions.map((question) => {
return mongoose.Types.ObjectId(question);
});

console.log(round.questions);
game.rounds.push(round);

return game.save()
}

参数游戏是:

{ 
teams: [],
rounds:
[ { categories: [Array],
questions: [],
_id: 5ac7507c5491ed422de3ce68,
roundNumber: 1 } ],
_id: 5ac74cccc65aac3e0c4b6cde,
code: '537epG',
__v: 1
}

参数轮为:

{ 
roundNumber: 1,
questions: [ '5ac5cfb41fca8a22f519cb22' ],
categories: [ 'Art and Literature', 'Music', 'Science and Nature' ]
}

console.log(round.questions) 结果:

[ 5ac5cfb41fca8a22f519cb22 ]

Mongoose :5.0.12,

我不知道我在这里做错了什么。并在此感谢您的帮助。

最佳答案

试试这个..

const roundSchema = Schema({
roundNumber: {
type: Number,
required: true,
},
categories: {
type: [String],
required: true
},
questions: [{type: Schema.Types.ObjectId, ref: 'Question', required: true}]
});

关于node.js - Mongoose:为值 "[ 5ac5cfb41fca8a22f519cb22 ]"转换为数组失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com