dan*_*lad 6 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;
Run Code Online (Sandbox Code Playgroud)
//app.js
var result = User.findOneByCredinitals({principal: 'admin',creditinal:'danieladenew'});
console.log("returned "+result);
Run Code Online (Sandbox Code Playgroud)
这是 app.js 的结果
------------------------------------------------
returned undefined
inside then function User Sequelize Instance i.e Useriffound and displayed.
Run Code Online (Sandbox Code Playgroud)
我如何将用户对象从那时返回到 app.js 代码?
您需要返回承诺才能显示数据。
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;
}
});
},
Run Code Online (Sandbox Code Playgroud)
另外,添加一个catch块可能是个好主意。如果查询失败,你将不知道其他情况
| 归档时间: |
|
| 查看次数: |
7660 次 |
| 最近记录: |