Node.js 当数据传递到Mongo模式时,数组元素丢失
我正在使用mongo和express构建一个测验应用程序 下面是我的模式的样子Node.js 当数据传递到Mongo模式时,数组元素丢失,node.js,arrays,mongodb,mongoose,Node.js,Arrays,Mongodb,Mongoose,我正在使用mongo和express构建一个测验应用程序 下面是我的模式的样子 const mongoose = require("mongoose") const Schema = mongoose.Schema const quizSchema = new Schema({ course: { type: String, required: true, }, title: { type: String, required:
const mongoose = require("mongoose")
const Schema = mongoose.Schema
const quizSchema = new Schema({
course: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
options: [
{ type: String, required: true },
{ type: String, required: true },
{ type: String },
{ type: String },
{ type: String },
],
module.exports = Quiz = mongoose.model("Fifth_question", quizSchema)
例如,我有一个对象如下
{
course: 'fruit',
title: 'Laravel: Blade',
option: [ 'orange', 'apple', 'grape', 'mango' ],
answer: 'orange'
}
当我把这个对象传递给下面的schema liek
const quiz = new quiz(obj)
它释放数组元素,返回空数组
这些是我在网上找到的,但仍然不起作用。
同样的结果
options: {
type: Array,
default: []
}
or
options: [
{ option1: { type: String, required: true } },
{ option2: { type: String, required: true } },
{ option3: String },
{ option4: String },
{ option5: String },
],
有人能告诉我我错过了什么吗
还有最后一个,对象数组,如果我们不想用字符串数组和对象数组,我应该如何修复代码
事先非常感谢可能只是一个打字错误。您的模式定义了
选项
(复数),但您的对象有选项
(单数)可能只是一个输入错误。您的模式定义了选项
(复数),但您的对象具有选项
(单数)