User.create 不是 Graphql 和 Sequelize 中的函数错误

时间:2021-05-22 14:05:02

标签: javascript node.js express graphql sequelize.js

当我想创建用户时,我不断收到 Graphql (GUI) 中 User.create is not a functionCannot read property 'create' of undefined 的错误。

错误: enter image description here

续集中的模型:

"use strict";
const bcrypt = require("bcrypt");
const { Model } = require("sequelize");
const { server } = require("../../config/server");

module.exports = (sequelize, DataTypes) => {
  const { STRING, DATE, DECIMAL, INTEGER } = DataTypes;

  class user extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      user.hasMany(models.Role, {
        foreignKey: "userId",
        through: "user_role",
      });
      user.hasMany(models.Company, {
        foreignKey: "userid",
        targetKey: "userid",
      });
    }
  }

  user.init(
    "user",
    {
      uuid: {
        type: STRING(50),
        allowNull: false,
        primaryKey: false,
      },
      firstName: {
        type: STRING(50),
        required: true,
        allowNull: false,
        field: "first_name",
      },
      lastName: {
        type: STRING(50),
        required: true,
        allowNull: false,
        field: "last_name",
      },
      username: {
        type: STRING(50),
        required: true,
        unique: true,
        allowNull: false,
      },
      password: {
        type: STRING(50),
        required: true,
        allowNull: false,
      },
      email: {
        type: STRING(50),
        required: true,
        allowNull: false,
      },
      totalBudget: {
        type: DECIMAL(20, 2),
        required: true,
        field: "total_budget",
      },
      usedBudget: {
        type: DECIMAL(20, 2),
        required: true,
        field: "used_budget",
      },
      phone: {
        type: INTEGER,
        required: true,
        allowNull: false,
      },
      createdAt: {
        type: DATE,
        allowNull: false,
        defaultValue: new Date(),
        field: "created_at",
      },
      updatedAt: {
        type: DATE,
        allowNull: false,
        defaultValue: new Date(),
        field: "updated_at",
      },
    },
    {
      sequelize,
      modelName: "user",
      defaultScope: {
        rawAttributes: { exclude: ["password"] },
      },
    }
  );

  user.beforeCreate(async (user) => {
    const hashed_password = bcrypt.hashSync(user.password, server.saltrounds);
    user.password = hashed_password;
  });

  return user;
};

GraphQL 架构:

const { gql } = require("apollo-server-express");

module.exports = gql`
  type User {
    id: Int!
    uuid: String!
    firstName: String
    lastName: String!
    username: String!
    password: String!
    email: String!
    totalBudget: Float!
    usedBudget: Float!
    phone: Int!
    # company: [Company!]
    # role: [Role!]
  }

  extend type Mutation {
    register(input: RegisterInput!): RegisterResponse
    login(input: LoginInput!): LoginResponse
  }

  type RegisterResponse {
    id: Int!
    firstName: String
    lastName: String!
    username: String!
    password: String!
    email: String!
    totalBudget: Float!
    usedBudget: Float!
    phone: Int!
    # company: [Company!]
    # role: [Role!]
  }

  input RegisterInput {
    firstName: String
    lastName: String!
    username: String!
    password: String!
    email: String!
    totalBudget: Float!
    usedBudget: Float!
    phone: Int!
    # company: [Company!]
    # role: [Role!]
  }

  input LoginInput {
    email: String!
    password: String!
  }

  type LoginResponse {
    firstName: String
    lastName: String!
    username: String!
    password: String!
    email: String!
    totalBudget: Float!
    usedBudget: Float!
    phone: Int!
    # company: [Company!]
    # role: [Role!]
    token: String!
  }
`;


GraphQL 解析器:

const bcrypt = require("bcrypt");
const { AuthenticationError } = require("apollo-server-express");

const User = require("../../database/models/").user;
const { toJWT } = require("../../config/JWT");
const { server } = require("../../config/server");

module.exports = {
  Mutation: {
    async register(root, args, context) {
      const {
        firstName,
        lastName,
        username,
        password,
        email,
        totalBudget,
        usedBudget,
        phone,
      } = args.input;
      console.log(
        firstName,
        lastName,
        username,
        password,
        email,
        totalBudget,
        usedBudget,
        phone
      );

      const userCreated = User.create({
        firstName,
        lastName,
        username,
        password,
        email,
        totalBudget,
        usedBudget,
        phone,
      });

      console.log(userCreated);
      return userCreated;
    },

    async login(root, { input }, context) {
      const { email, password } = input;
      const user = await User.findOne({ where: { email } });
      if (user && bcrypt.compareSync(password, user.password)) {
        const token = toJWT({ id: user.id }, secret, { expiresIn });
        return { ...user.toJSON(), token };
      }
      throw new AuthenticationError("Invalid credentials");
    },
  },
};

如果我在“User.create is not a function”中更改导入错误更改的方式,则每次发布时我都会收到错误消息。我已经尝试了几件事,但由于某种原因我无法让它工作。

0 个答案:

没有答案
相关问题