如何解决编译后无法覆盖模型-在模型A的静态变量中引用模型B时出现猫鼬错误
我正在尝试创建一个简单的测验应用程序来娱乐。我希望用户能够编写一个测验,将其保存,然后启动它,这将生成带有房间代码的测验实例。然后,团队/用户可以输入他们的团队名称和房间代码,然后他们将加入该测验实例。
我遇到了模型覆盖错误的问题,因为当我要执行基本的登录功能(但使用团队名称和房间代码而不是用户名和密码)时,我想当团队想要参加测验时,检查Team
集合中是否存在该团队,并且QuizInstance
集合中是否存在一个带有提供的房间代码的QuizInstance
,如果存在,则将其添加到QuizInstance's
团队中数组。
我正在将此功能作为Team模型的静态运行,如下所示:
teamSchema.statics.findByCredentials = async (teamname,roomcode) => {
const team = await User.findOne({ teamname });
if (!team) {
throw new Error({ error: "Invalid login credentials" });
}
const quizInstance = await QuizInstance.findOne({ roomcode });
if (!quizInstance) {
throw new Error({ error: "Invalid room code" });
}
const isPasswordMatch = await bcrypt.await(roomcode,quizInstance.roomcode);
if (!isPasswordMatch) {
throw new Error({ error: "Invalid login credentials" });
}
//TODO: add this team to the quizInstance's `teams` array.
return team;
};
这是问题所在。我对猫鼬不太了解,所以如果我错了,请纠正我,但这是我的欠缺,因为我在团队模型中调用require
时使用了QuizInstance模型,因为它是自己的控制器(QuizInstanceController
)正在尝试重新编译模型,并发现该模型已经由Team模型完成。那是对的吗?如果可以,我该如何解决?预先感谢。
下面是我的代码:
QuizInstance模型
var mongoose = require("mongoose");
const bcrypt = require("bcryptjs");
var Schema = mongoose.Schema;
var quizInstanceSchema = new Schema({
quiz: { type: Schema.Types.ObjectId,required: true,ref: "Quiz" },teams: [{ type: Schema.Types.ObjectId,ref: "Team" }],startDate: { type: Date,required: true },questionInstances: [
{ type: Schema.Types.ObjectId,ref: "QuestionInstance" },],roomCode: { type: Schema.Types.Number,});
quizInstanceSchema.set("toObject",{ virtuals: true });
quizInstanceSchema.set("toJSON",{ virtuals: true });
quizInstanceSchema.pre("save",async function (next) {
const quizInstance = this;
if (quizInstance.isModified("roomCode")) {
quizInstance.roomCode = await bcrypt.hash(quizInstance.roomCode,8);
}
next();
});
module.exports = mongoose.model("QuizInstance",quizInstanceSchema);
团队模型
const mongoose = require("mongoose");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
let QuizInstance = require("./quizInstance");
const teamSchema = mongoose.Schema({
name: {
type: String,trim: true,},password: {
type: String,minLength: 7,type: {
type: String,enum: ["Admin","Team"],members: [
{ type: mongoose.Schema.Types.ObjectId,ref: "Member",required: false },tokens: [
{
token: {
type: String,quizzesTaken: [
{
type: mongoose.Schema.Types.ObjectId,ref: "QuizInstance",required: false,});
teamSchema.methods.generateAuthToken = async function () {
const team = this;
const token = jwt.sign({ _id: team._id },process.env.JWT_KEY);
team.tokens = team.tokens.concat({ token });
await team.save();
return token;
};
teamSchema.statics.findByCredentials = async (teamname,quizInstance.roomcode);
if (!isPasswordMatch) {
throw new Error({ error: "Invalid login credentials" });
}
//TODO: add this team to the quizInstance's `teams` array.
return team;
};
module.exports = mongoose.model("Team",teamSchema);
QuizInstanceController(第1行是引发错误的位置)
const QuizInstance = require("../models/QuizInstance");
exports.getQuizInstance = (req,res,next) => {
QuizInstance.findOne({ _id: req.params.id }).exec((err,qi) => {
if (err) return res.status(500).json({ success: false,error: err });
if (!qi)
return res
.status(404)
.json({ success: false,error: "Could not find QuizInstance" });
return res.status(200).json({ success: true,quizInstance: qi });
});
};
//...more CRUD functions...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。