最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to wait on sequelize executing a findOne - Stack Overflow

programmeradmin3浏览0评论

I've got a route using Sequelize.js

app.get('/api/users/:username', (req, res) => {
  const foundUser = getUserByUsername(req.params.username);
  console.log(`foundUser = ${foundUser}`);
  return res.send(foundUser);
});

the getUserByUsername function is as follows

const getUserByUsername = username => {
  Viewer.findOne({
    where: {username}
  }).then(response => {
    console.log(response.dataValues);//the object with the data I need
    return response.dataValues;
  });
};

I hoped on getting the object in my const foundUser in my route, but it seems I need to wait until the findOne has been executed, because in my console I can see that the log of foundUser (which is undefined then) is executed before the function getUserByUsername

foundUser = undefined
Executing (default): SELECT `id`, `username`, `instakluiten`, `role`, `createdAt`, `updatedAt` FROM `viewers` AS `viewer` WHERE `viewer`.`username` = 'instak' LIMIT 1;
{ id: 19,
  username: 'instak',
  instakluiten: 18550,
  role: 'moderators',
  createdAt: 2016-10-02T16:27:44.000Z,
  updatedAt: 2016-10-09T10:17:40.000Z }

How can I make sure that my foundUser will be updated with the data áfter the user has been found?

I've got a route using Sequelize.js

app.get('/api/users/:username', (req, res) => {
  const foundUser = getUserByUsername(req.params.username);
  console.log(`foundUser = ${foundUser}`);
  return res.send(foundUser);
});

the getUserByUsername function is as follows

const getUserByUsername = username => {
  Viewer.findOne({
    where: {username}
  }).then(response => {
    console.log(response.dataValues);//the object with the data I need
    return response.dataValues;
  });
};

I hoped on getting the object in my const foundUser in my route, but it seems I need to wait until the findOne has been executed, because in my console I can see that the log of foundUser (which is undefined then) is executed before the function getUserByUsername

foundUser = undefined
Executing (default): SELECT `id`, `username`, `instakluiten`, `role`, `createdAt`, `updatedAt` FROM `viewers` AS `viewer` WHERE `viewer`.`username` = 'instak' LIMIT 1;
{ id: 19,
  username: 'instak',
  instakluiten: 18550,
  role: 'moderators',
  createdAt: 2016-10-02T16:27:44.000Z,
  updatedAt: 2016-10-09T10:17:40.000Z }

How can I make sure that my foundUser will be updated with the data áfter the user has been found?

Share Improve this question asked Oct 9, 2016 at 10:30 KevinKevin 1,3872 gold badges17 silver badges35 bronze badges 2
  • You can't return result like this, you need to write your code in callback after that you will get it – abdulbari Commented Oct 9, 2016 at 10:33
  • use callback function – Umakant Mane Commented Oct 9, 2016 at 10:39
Add a ment  | 

3 Answers 3

Reset to default 2

You have to return the promise that Sequelize creates and then wait for it to resolve. So the getUserByUsername bees:

const getUserByUsername = username => {
  return Viewer.findOne({
    where: {username}
  }).then(response => {
    console.log(response.dataValues);//the object with the data I need
    return response.dataValues;
  });
};

and in the route:

app.get('/api/users/:username', (req, res) => {
  getUserByUsername(req.params.username).then(foundUser => {
    res.send(foundUser);
  });
});

This is because you need to keep the chain of promises. If you forget to return it, the function returns undefined end even if the promise is finallly resolved, the value it resolves to never gets up back in the chain.

app.get('/api/users/:username', (req, res) => {
getUserByUsername(req.params.username, function(err, result){
const foundUser = result;
console.log(`foundUser = ${foundUser}`);
 res.send(foundUser); 
});

});



const getUserByUsername = function(username, callback) {
Viewer.findOne({
where: {username}
}).then(response => {
console.log(response.dataValues);//the object with the data I need
return callback(null, response.dataValues);
});
};

You can avoid it with promise or with callback

app.get('/api/users/:username', (req, res) => {
  getUserByUsername(req.params.username, function(err, foundUser) {
    if (!err) {
      console.log(`foundUser = ${foundUser}`);
      return res.send(foundUser);
    } else {
      res.send(err)
    }
  });
});



const getUserByUsername = (username, callback) => {
  Viewer.findOne({
    where: {
      username
    }
  }).then(response => {
    console.log(response.dataValues); //the object with the data I need
    return callback(null, response.dataValues);
  });
};
发布评论

评论列表(0)

  1. 暂无评论