1

Using Sequelizefor the first time I'm trying this code in my Node router:

router.get('/get-users', async (req, res, next) => {
  const data = await users.getAll()
  console.log('users in router:', data);
  res.send(data)
});

And this is the sequlize code:

const users = {
  getAll () {
    sequelize
      .query('SELECT * FROM users', { model: User })
      .then(users => {
        console.log('users in database:', users);
        return users;
      })
  }
};

The log statement in Sequelize code is getting me the right data but the log statement in router gets undefined.

It's probably async await problem but can you help?

1 Answer 1

1

Try to return the promise in the getAll(). Await is just a wrapper for a then() call of a promise but since you are not returning a promise the await is not waiting for the promise.

  getAll () {
    return sequelize
      .query('SELECT * FROM users', { model: User })
      .then(users => {
        console.log('users in database:', users);
        return users;
    })
  }
Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thank you! Yes I forgot to the part of returning a promise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.