SequelizeJS错误:TypeError:User.findAll不是函数

时间:2017-05-27 16:06:47

标签: javascript node.js express sequelize.js

我正在使用sequelizejs和expressjs app。

这是我的用户模型的代码。

user.js的

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
    bio: DataTypes.STRING,
    email: DataTypes.STRING,
    profile_picture: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  }, {
    tableName: 'users'
  });
  return User;
};

这是我的用户控制器

userComponent.js

const express = require( 'express' )
const router  = express.Router()
const User    = require( '../../models/user' )

router.get( '/user', ( req, res, next ) => {
  User.findAll()
    .then( userResponse => {
      res.status( 200 ).json( userResponse )
    } )
    .catch( error => {
      res.status( 400 ).send( error )
    } )
} )

module.exports = router

但是当我试图获取数据时,它会抛出这个错误:

  

TypeError:User.findAll不是函数       在router.get(D:\ StateBoard-Project \ v2 \ components \ user \ userComponent.js:6:8)       在Layer.handle [as handle_request](D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ layer.js:95:5)       在下一个(D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ route.js:137:13)       在Route.dispatch(D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ route.js:112:3)       在Layer.handle [as handle_request](D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ layer.js:95:5)       在D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ index.js:281:22       在Function.process_params(D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ index.js:335:12)       在下一个(D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ index.js:275:10)       在Function.handle(D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ index.js:174:3)       在路由器(D:\ StateBoard-Project \ v2 \ node_modules \ express \ lib \ router \ index.js:47:12)

enter image description here

这是由Sequelize自动生成的model / index.js文件。

const fs        = require( 'fs' )
const path      = require( 'path' )
const Sequelize = require( 'sequelize' )
const basename  = path.basename( module.filename )
const env       = process.env.NODE_ENV || 'development'
const config    = require( __dirname + '/..\database.json' )[env]
const db        = {}

let sequelize
if (config.use_env_variable) {
  sequelize = new Sequelize( process.env[ config.use_env_variable ] )
} else {
  sequelize = new Sequelize( config.database, config.username, config.password, config )
}

fs
  .readdirSync( __dirname )
  .filter( function( file ) {
    return ( file.indexOf( '.' ) !== 0 ) && ( file !== basename ) && ( file.slice( -3 ) === '.js' )
  } )
  .forEach( function( file ) {
    var model = sequelize[ 'import' ]( path.join( __dirname, file ) )
    db[ model.name ] = model
  });

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

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

这里缺少什么?

3 个答案:

答案 0 :(得分:7)

您应该要求models / index.js使用模型,而不是模型/ user.js。

const models = require( '../../models/index');

router.get( '/user', ( req, res, next ) => {
  models.User.findAll()
    .then( userResponse => {
      res.status( 200 ).json( userResponse )
    })
    .catch( error => {
      res.status( 400 ).send( error )
    })
} )

答案 1 :(得分:2)

您的 user.js 文件正在导出一个函数,但您从未实例化它。

module.exports = function(sequelize, DataTypes) { // <-- function

当你需要它时

const User = require('../../models/user')

现在User是对该功能的引用。它从哪里得到sequelizeDataTypes

答案 2 :(得分:0)

您应该导入用户模型文件。

feature_id