从那里获得返回结果

我有以下代码:

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 ie Useriffound and displayed. 

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

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

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

另外,添加一个catch块也是一个好主意。 如果查询失败,您将不知道其他情况