简体   繁体   中英

Cannot read properties of undefined (reading 'findOne') (in sequelize model)

I tried logging in via postman. but it has been discontinued. like that在此处输入图像描述

I used the console log for the corresponding problem line, and the model user was undefined. But I've connected it properly and I'm not sure why it's an error.

my code is like that

index.js

const { user } = require("../../entities/models/user");

    console.log('login 2:',user)
    const userInfo = await user.findOne({
      where: { email: req.body.email, social: 'google'}
    })
models/user.js

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, 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) {
      // define association here
    }
  };
  user.init({
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING,
    social: DataTypes.STRING,
    socialid: DataTypes.STRING,
    gender: DataTypes.STRING,
    age: DataTypes.INTEGER,
    height: DataTypes.STRING,
    weigt: DataTypes.STRING,
    profileimage: DataTypes.STRING,
    total_time: DataTypes.INTEGER
  }, {
    sequelize,
    modelName: 'user',
  });
  return user;
};

I don't understand. Obviously, when I click the path require("../../entities/models/user"); , it works well with the model, but why does undefined keep popping up?

You are exporting whole module from models/user.js so you need to import it like this:

const user = require("../../entities/models/user");

Also, you need to call user with (sequelize, DataTypes), so:

const Sequelize = require('sequelize');
const sequelize = new Sequelize(/*your config here*/)
const user = require("../../entities/models/user")(sequelize, Sequelize);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM