Javascript Sequelizejs承诺返回空对象
我正在使用Sequelizejs,并尝试创建简单的用户验证函数Javascript Sequelizejs承诺返回空对象,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,我正在使用Sequelizejs,并尝试创建简单的用户验证函数 function ValidateIsUserLogged(sessionKeyInput) { var result = app.get('dbContext').user .find({ where: { sessionKey: sessionKeyInput } }) .then(function (us
function ValidateIsUserLogged(sessionKeyInput) {
var result = app.get('dbContext').user
.find({
where: {
sessionKey: sessionKeyInput
}
})
.then(function (user) {
return user != null;
}, function (error) {
console.log(error);
});
return result;
}
in.then()用户很好,它是同一个正确的对象,一切都很好,但我希望得到超出承诺的结果。我尝试了.success().error(),现在尝试了.then(),结果(几乎)是一样的。我得到了一个空对象,但我无法从promise中的函数获得结果。我尝试在.then()中设置一个变量,但结果是同一个变量没有更改其值,或者函数已经返回了值(在赋值/更改值之前)。我知道这是一种异步方法,也许问题是关于一般的承诺,但我已经在这里呆了好几天了。非常感谢您的帮助:)请编写同步等效程序:
function validateIsUserLogged(sessionKeyInput) {
try {
var user = app.get('dbContext').user
.find({
where: {
sessionKey: sessionKeyInput
}
});
}
catch(error) {
console.log(error);
}
return user != null;
}
修改它:
function validateIsUserLogged(sessionKeyInput) {
try {
var user = app.get('dbContext').user
.find({
where: {
sessionKey: sessionKeyInput
}
});
}
catch(error) {
console.log(error);
}
return user;
}
将上述内容转化为承诺:
function validateIsUserLogged(sessionKeyInput) {
return app.get('dbContext').user
.find({
where: {
sessionKey: sessionKeyInput
}
}).then(function(user) {
//This is pointless but I have no idea what you want to do
return user;
}, function(error) {
console.log(error);
});
}
我尝试查看会话密钥是否有效->因此,如果我找到一个不为null的用户,我将其作为true返回;在您的方法中,我再次获得空对象problem@Daemonyoyo我没有解决方案,但提示说,如果您编写的代码是同步的,并且解决了问题,那么可以直接转换为基于承诺的代码