如何修复“消息”:调用node.js中的API时收到“Cannot read properties of undefined(阅读'findOne')”并进行序列化?

mefy6pfw  于 2023-03-01  发布在  Node.js
关注(0)|答案(1)|浏览(318)

我已经为我糟糕的英语感到抱歉了,这是我在这里的第一个问题。我试图用API做一个登录应用程序,有2个角色(管理员,普通用户),每次我调用API时,这个消息都会出现在Thunder客户端:"message":"无法读取未定义的属性(正在读取" findOne ")"
下面是我的代码:
src/database/models/index.js

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const process = require('process');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {}

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}
db.sequelize = sequelize
fs
  .readdirSync(__dirname)
  .filter(file => {
    return (
      file.indexOf('.') !== 0 &&
      file !== basename &&
      file.slice(-3) === '.js' &&
      file.indexOf('.test.js') === -1
    );
  })
  .forEach((file) => {
    const model = require(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

/* db.sequelize = sequelize;
 */db.Sequelize = Sequelize;

module.exports = db;

src/database/models/User.js

'use strict';

const userModel = (sequelize, DataTypes) =\> {
    const User = sequelize.define('User', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        role: {
            type: DataTypes.STRING,
        },
        name: {
            type: DataTypes.STRING,
        },
        email: {
            type: DataTypes.STRING,
        },
        password: {
            type: DataTypes.STRING,
        },

    }, {
        tableName: 'users',
        underscored: true,
        timestamps: false,
    });

    return User
}

module.exports = { userModel };

src/service/login.service.js

const md5 = require('md5');
const { User } = require('../database/models');
const { generateToken } = require('../utils/JWT');
const errorThrower = require('../utils/errorThrower');

const login = async ({ email, password }) => {
  const encodedPassword = md5(password);
  console.log(User)
  const user = await User.findOne({ where: { email, password: encodedPassword } });

  if (!user) errorThrower(404, 'Incorrect email or password');

  const token = generateToken(user);
  const { name, email: emailUser, role } = user;

  return { name, email: emailUser, role, token };
};

module.exports = {
  login,
};

我很感激任何人试图帮助我,并为我的英语再次抱歉

owfi6suc

owfi6suc1#

您只需要导出模型函数本身,而不是导出带有指向此函数的 prop 的ab对象:

module.exports = userModel;

然后在使用require导入时调用它:

.forEach((file) => {
    const model = require(path.join(__dirname, file))(
       sequelize,
       Sequelize.DataTypes
    );
    db[model.name] = model;
  });

查看我的other answer here并比较型号注册码

相关问题