来自.then的续集结果

时间:2016-10-03 07:01:40

标签: node.js sequelize.js

我有以下代码:

var ORM = require('../helpers/mysql_orm');
var log = require('../helpers/logger');  

function UserModel() {

   this.User = ORM.define('User', {

        }, {
        tableName: 'User',
        timestamps: false,
        }); 
}


UserModel.prototype.findOneByCredinitals = function (creditinals) {

    this.User.findOne({ 
            attributes:['username','id'],
            where: 
             { 
              username:creditinals.principal,
              password:creditinals.creditinal}
            })
            .then(function(user) {
           console.log("inside then function"+user);
            return user;
    })
   // console.log(result);

},
UserModel.prototype.findAllByLimit = function(req,res) {


}


module.exports= new UserModel;

// app.js

var result = User.findOneByCredinitals({principal: 'admin',creditinal:'danieladenew'});

console.log("returned "+result);

这是app.js

的结果
------------------------------------------------
returned undefined
inside then function User Sequelize Instance i.e Useriffound and displayed.

如何将用户对象从那时返回到app.js代码?

1 个答案:

答案 0 :(得分:2)

您需要返回承诺才能显示数据。

UserModel.prototype.findOneByCredential = function (credentials) {

    return this.User.findOne({ 
        attributes: ['username', 'id'],
        where: { 
            username: credential.principal,
            password: credential.credential
        }
    }).then(function(user) {
            console.log("inside then function"+user);
            return user;
        }
    });
},

此外,添加catch块可能是个好主意。如果查询失败,则您将不知道