Node router can't get data from Sequelize using async await
up vote
1
down vote
favorite
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?
node.js asynchronous sequelize.js
add a comment |
up vote
1
down vote
favorite
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?
node.js asynchronous sequelize.js
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
node.js asynchronous sequelize.js
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?
node.js asynchronous sequelize.js
node.js asynchronous sequelize.js
asked Nov 9 at 14:08
Lee Moe
227
227
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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;
)
It works! Thank you! Yes I forgot to the part of returning a promise.
– Lee Moe
Nov 9 at 14:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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;
)
It works! Thank you! Yes I forgot to the part of returning a promise.
– Lee Moe
Nov 9 at 14:25
add a comment |
up vote
1
down vote
accepted
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;
)
It works! Thank you! Yes I forgot to the part of returning a promise.
– Lee Moe
Nov 9 at 14:25
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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;
)
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;
)
answered Nov 9 at 14:17
RetBack
411
411
It works! Thank you! Yes I forgot to the part of returning a promise.
– Lee Moe
Nov 9 at 14:25
add a comment |
It works! Thank you! Yes I forgot to the part of returning a promise.
– Lee Moe
Nov 9 at 14:25
It works! Thank you! Yes I forgot to the part of returning a promise.
– Lee Moe
Nov 9 at 14:25
It works! Thank you! Yes I forgot to the part of returning a promise.
– Lee Moe
Nov 9 at 14:25
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227286%2fnode-router-cant-get-data-from-sequelize-using-async-await%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password