gpt4 book ai didi

javascript - Mongoose 类型错误 : Question is not a constructor

转载 作者:太空宇宙 更新时间:2023-11-04 01:38:43 25 4
gpt4 key购买 nike

关于此错误,有很多关于堆栈溢出的问题。但他们的解决方案似乎对我不起作用。所以我最终决定发布我自己的问题。 (这可能与我缺乏 javascript/node/mongoose 知识有关)

我的模块是 -

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

var OptionSchema = new Schema({
option_text: {type: String, required: true},
is_correct: {type: Boolean, required: true, default: false}
});

var QuestionSchema = new Schema({
question_text: {type: String, required: true},
subText: {type: String, required: true},
type: {type: String, required: true},
difficulty: {type: String, required: true, default: 'Easy'},
status: {type: String, required: true, default: 'Private'},
options_required: {type: Boolean, required: true, default: false},
explanation: {type: [String], required: true},
created_by: {type: String, required: true, default: 'Anonymous'},
created_at: {type: Date, required: true, default: Date.now()},
options: [OptionSchema]
});



// Export the model
module.exports.Question = QuestionSchema;
module.exports.Option = OptionSchema;

我的 Controller 有这个 -

//create
exports.question_create = function (req, res, next) {
let qu = new Question(
{
question_text: req.body.question_text,
subText: req.body.subText,
type: req.body.type,
difficulty: req.body.difficulty,
status: req.body.status,
options_required: req.body.options_required,
explanation: req.body.explanation,
created_by: '',
created_at: Date.now(),
option: req.body.option
}
);

qu.save(function (err) {
if (err) {
return next('some error occurred ' + err);
}
res.send('question Created successfully')
})
};

我能找到的最接近的问题是在此链接上 - Mongoose TypeError: User is not a constructor

但答案不起作用。

_/\_

最佳答案

我认为你需要按照以下方式做一些事情:

module.exports.Question = mongoose.model('Question', QuestionSchema);

关于javascript - Mongoose 类型错误 : Question is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53671473/

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