I have a situation where I want to access the user data only if he or she is logged in. I have the following code that works when I'm not querying the database. Meaning the var tmp
is set to YOUR_API_KEY
. But when querying the database, I get [object Promise]. I tried different solutions after googling around, but nothing seams to solve my question.
var tmp = function() {
if(req.session.user) {
return User.findOne({
where: {
id: req.session.user.userID
},
include: [
{ model: Apikey }
]
}).then(function(sqlUser) {
return sqlUser.Apikeys['0'].kay;
});
} else {
return "YOUR_API_KEY"
}
}
Use "callbacks"
var tmp = function(callback) {
if(req.session.user) {
return User.findOne({
where: {
id: req.session.user.userID
},
include: [
{ model: Apikey }
]
}).then(function(sqlUser) {
callback(sqlUser.Apikeys['0'].kay)
});
} else {
callback("YOUR_API_KEY")
}
}
//now you can use your function like this
tmp(function(apiKey){
//do something with apiKey e.g.:
console.log(apiKey)
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.