如何修复“猫鼬模型的名称"变异时不是graphql中的构造函数 [英] How to fix the "Name of the mongoose Model" is not a constructor in graphql when mutating

查看:26
本文介绍了如何修复“猫鼬模型的名称"变异时不是graphql中的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

graphql 操场中的变异数据表明消息:学生不是构造函数错误:类型错误:学生不是构造函数"学生是我的猫鼬模型.

Mutating data in the graphql playground says the message of Message: Student is not a constructor Error: "TypeError: Student is not a constructor" Student is my Mongoose Model.

我尝试重新安装我的 node_modules,在 github 上搜索了一些修复.

I've tried reinstalling my node_modules, searching some fix on the 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);

应该创建学生,但该错误会弹出该消息.

The student should be created, but that error pops up with that message.

推荐答案

已修复!我应该提供查询变量而不是手动输入 graphql 突变

Fixed! I should have provided query variables rather than manual typing on the graphql mutation

这篇关于如何修复“猫鼬模型的名称"变异时不是graphql中的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆