Skip to content Skip to sidebar Skip to footer

Sequelize: How To Access [object Promise] Or Return Value From Promise?

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

Solution 1:

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)
});

Post a Comment for "Sequelize: How To Access [object Promise] Or Return Value From Promise?"