2017-10-18 5 views
0

sequelize + mysql + express를 사용하여 간단한 프로젝트를 개발 중이며 간단한 문제로 인해 막혔습니다. 내 모델을 사용하여 findById와 같은 기능을 실행할 수 없습니다.내 sequelize 모델에서 아무 작업도 수행 할 수 없습니다

다음 코드에서 "db.usuario.findById (...). exec는 함수가 아닙니다."메시지가 나타납니다. 나는 초보자입니다.

내 모델 :

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', '[email protected]', { 
    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; 

답변

0

이 코드가 혼합 것 같다 Sequelize와 Mongoose 모두 ​​findById :

이라는 메서드가 있습니다.
db.usuario.findById(req.params.id) 
    .exec(callback) 

몽구스는 exec을 사용하는 반면, Sequelize 메서드 findById은 Promise를 반환합니다. 실제로는 async.parallel을 사용할 필요가 없으며 대신 Promise.all을 사용할 수 있습니다 (실제 코드에 여러 개의 쿼리가 포함되어 있다고 가정합니다. 실제로이 중 하나가 필요하면이 중 하나가 필요하지 않습니다).

당신이 async.parallel를 사용하여이 작업을 수행하는 경우는 다음과 같이 보일 수 있습니다 :

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