变异时如何修复 "Name of the mongoose Model" 不是 graphql 中的构造函数
How to fix the "Name of the mongoose Model" is not a constructor in graphql when mutating
graphql playground 中的变异数据表示消息
消息:学生不是构造函数
错误:"TypeError: Student is not a constructor"
学生是我的猫鼬模型。
我已尝试重新安装我的 node_modules,在 github 上搜索一些修复程序。
这是我的变异函数
addStudent: async (
root,
{ studentId, firstName, lastName, email, password },
{ Student }
) => {
const newStudent = await new Student({
studentId,
firstName,
lastName,
email,
password
}).save();
return newStudent;
}
这是我的猫鼬模型
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const StudentSchema = new Schema({
studentId: {
type: String,
required: true
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
// sectionId: {
// type: [Schema.Types.ObjectId],
// ref: "Section",
// nullable: true
// },
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
createdDate: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model("Student", StudentSchema);
应该创建学生,但该消息会弹出该错误。
Await working on promise 但这里你传递的是学生对象
所以它 returns Student 不是构造函数
const newStudent = await new Student({
studentId,
....
}).save();
相反,您可以这样做
1) 使用 append
创建学生对象
const newStudent = new Student({})
newStudent.studentId = studentId
newStudent.firstName = firstName
newStudent.lastName = lastName
newStudent.email = email
newStudent.password = password
2) 使用构造函数创建学生对象
const newStudent = new Student({
studentId,
firstName,
lastName,
email,
password
})
并使用 promise async 和 await
保存
await newStudent.save()
或
const newStudent = await Student.create({
studentId,
firstName,
lastName,
email,
password
})
已修复!我应该提供查询变量而不是手动输入 graphql 突变
graphql playground 中的变异数据表示消息 消息:学生不是构造函数 错误:"TypeError: Student is not a constructor" 学生是我的猫鼬模型。
我已尝试重新安装我的 node_modules,在 github 上搜索一些修复程序。
这是我的变异函数
addStudent: async (
root,
{ studentId, firstName, lastName, email, password },
{ Student }
) => {
const newStudent = await new Student({
studentId,
firstName,
lastName,
email,
password
}).save();
return newStudent;
}
这是我的猫鼬模型
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const StudentSchema = new Schema({
studentId: {
type: String,
required: true
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
// sectionId: {
// type: [Schema.Types.ObjectId],
// ref: "Section",
// nullable: true
// },
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
createdDate: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model("Student", StudentSchema);
应该创建学生,但该消息会弹出该错误。
Await working on promise 但这里你传递的是学生对象 所以它 returns Student 不是构造函数
const newStudent = await new Student({
studentId,
....
}).save();
相反,您可以这样做
1) 使用 append
创建学生对象const newStudent = new Student({})
newStudent.studentId = studentId
newStudent.firstName = firstName
newStudent.lastName = lastName
newStudent.email = email
newStudent.password = password
2) 使用构造函数创建学生对象
const newStudent = new Student({
studentId,
firstName,
lastName,
email,
password
})
并使用 promise async 和 await
保存 await newStudent.save()
或
const newStudent = await Student.create({
studentId,
firstName,
lastName,
email,
password
})
已修复!我应该提供查询变量而不是手动输入 graphql 突变