这些是我的架构
// in test schema file
const Test = new mongoose.Schema(
{
_id: { type: String, default: uuid.v1 },
responses: [Response]
},
{
timestamps: true
}
);
// in response schema file
const Response = new mongoose.Schema({
_id: { type: String, default: uuid.v1 },
answer: {
type: String,
enum: [
"StronglyAgree",
"Agree",
"SomewhatAgree",
"Neutral",
"SomewhatDisagree",
"Disagree",
"StronglyDisagree"
]
},
question: { type: String, ref: "Question" }
});
我尝试输入对我的测试的反应,如此
async create() {
try {
const test = new this.model({ _id: uuid.v1() });
const questions = await this.questionModel.getAllQuestions();
questions.forEach(question => {
test.responses.push({
question: question._id,
answer: "StronglyAgree"
});
});
var savedTest = await test.save();
savedTest = await this.model.populate(test, {
path: "responses.question"
});
return savedTest;
} catch (e) {
throw e;
}
}
我收到此错误
{ responses:
MongooseError {
message: 'Cast to Array failed for value "1" at path "responses"',
name: 'CastError',
stringValue: '"1"',
kind: 'Array',
value: 1,
path: 'responses',
reason: [Object] } },
_message: 'Test validation failed',
name: 'ValidationError' }
为什么会这样?