【问题标题】:Can't execute any operations on my sequelize model无法对我的续集模型执行任何操作
【发布时间】:2018-03-29 19:31:40
【问题描述】:

我正在使用 sequelize + mysql + express 开发一个简单的项目,但由于一个简单的问题而陷入困境:无法使用我的模型执行 findById 等函数。

对于以下代码,我收到“db.usuario.findById(...).exec is not a function”消息。我是新手。

这是我的模型:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('usuario', {
    id_Usuario: {
      type: DataTypes.STRING(18),
      allowNull: false,
      primaryKey: true
    },
    tipo: {
      type: DataTypes.STRING(1),
      allowNull: false
    },
    nome: {
      type: DataTypes.STRING(45),
      allowNull: false
    },
    matricula: {
      type: DataTypes.STRING(45),
      allowNull: false,
      unique: true
    },
    telefone: {
      type: DataTypes.STRING(15),
      allowNull: true
    },
    cpf: {
      type: DataTypes.STRING(11),
      allowNull: false,
      unique: true
    },
    email: {
      type: DataTypes.STRING(45),
      allowNull: false,
      unique: true
    },
    senha: {
      type: DataTypes.STRING(45),
      allowNull: false
    },
    instituicaoEnsino: {
      type: DataTypes.STRING(45),
      allowNull: false
    }
  }, {
    tableName: 'usuario'
  });
};

这是我的控制器:

var db = require('../db');

exports.user_edit_get = function(req, res, next) {

    async.parallel({
        user: function(callback) {
            db.usuario.findById(req.params.id)
              .exec(callback)
        },
    }, function(err, results) {
        if (err) { return next(err); }
        //Successful, so render
        res.render('usuarioDetalhes', { user } );
    });

}

我的 db.js 文件:

var Sequelize = require('sequelize');

const sequelize = new Sequelize('mydb', 'root', 't3st3@B4NCO00', {
  host: 'localhost',
  port: '3306',
  dialect: 'mysql',

  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
});

// Connect all the models/tables in the database to a db object,
//so everything is accessible via one object
const db = {};

db.Sequelize = Sequelize;
db.sequelize = sequelize;

//Models/tables
db.usuario = require('./models/usuario.js')(sequelize, Sequelize);

module.exports = db;

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:

    这段代码好像是Sequelize和Mongoose的混合体,两者都有一个叫findById的方法:

    db.usuario.findById(req.params.id)
      .exec(callback)
    

    Mongoose 使用 exec,而 Sequelize 方法 findById 将返回一个 Promise。真的没有必要使用async.parallel,你可以使用Promise.all 代替(我假设你的真实代码包含多个查询,如果你真的只有1那么你不需要任何这些)。

    如果您使用async.parallel 执行此操作,它可能看起来像这样:

    async.parallel({
        user: function(callback) {
            db.usuario.findById(req.params.id).then(
                function(value) {
                    callback(null, value);
                },
                function(err) {
                    callback(err);
                }
            );
        }
    }, ...
    

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 2017-05-03
      • 2015-05-23
      • 2022-01-24
      • 2015-05-19
      • 2014-02-17
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多