Sequelize + Express TypeError:User.find不是一个函数

时间:2019-05-28 14:49:13

标签: node.js express sequelize.js

我正在关注有关using PostgreSQL, Express and Passport的在线教程,当我尝试登录时,出现以下错误堆栈跟踪:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
            .antMatchers(SWAGGER_URL)
            .antMatchers(SWAGGER_HTML);
}

/path/to/server/models/index.js

at /path/to/server/routes/api.js:30:8
at Layer.handle [as handle_request] (/path/to/server/node_modules/express/lib/router/layer.js:95:5)
at next (/path/to/server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/path/to/server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/path/to/server/node_modules/express/lib/router/layer.js:95:5)
at /path/to/server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/path/to/server/node_modules/express/lib/router/index.js:335:12)
at next (/path/to/server/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/path/to/server/node_modules/express/lib/router/index.js:174:3)
at router (/path/to/server/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/path/to/server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/path/to/server/node_modules/express/lib/router/index.js:317:13)
at /path/to/server/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/path/to/server/node_modules/express/lib/router/index.js:335:12)
at next (/path/to/server/node_modules/express/lib/router/index.js:275:10)
at /path/to/server/node_modules/express/lib/router/index.js:635:15
at next (/path/to/server/node_modules/express/lib/router/index.js:260:14)
at Function.handle (/path/to/server/node_modules/express/lib/router/index.js:174:3)
at router (/path/to/server/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/path/to/server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/path/to/server/node_modules/express/lib/router/index.js:317:13)
at /path/to/server/node_modules/express/lib/router/index.js:284:7

/path/to/server/models/user.js

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[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);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](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;

/path/to/server/api.js

'use strict';

var bcrypt = require('bcryptjs');

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    username: DataTypes.STRING,
    password: DataTypes.STRING
  }, {});
  User.beforeSave((user, options) => {
    if (user.changed('password')) {
      user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
    }
  });
  User.prototype.comparePassword = function (passw, cb) {
    bcrypt.compare(passw, this.password, function (err, isMatch) {
        if (err) {
            return cb(err);
        }
        cb(null, isMatch);
    });
  };
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

为什么User.find()不被识别为方法?以及如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我一直在寻找序列化文档上的find方法,却找不到find方法,只是findAllfindOne。也许在较新版本的docs.sequelizejs.com/manual/querying.html

上已弃用

答案 1 :(得分:0)

如前所述,这是因为它现在已被弃用。

模型部分中的 official guide to upgrade sequelize 到 v5,删除了别名子部分。