Using Sequelize with NodeJS and findByPk return pending












0















Hello I try use Sequelize and I need return a Object in DB, this is a piece of my code;



Simple like that:



const { User, Appointment } = require('../models')
const moment = require('moment')
const { Op } = require('sequelize')

class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = appoitments.map(appoint => {
const user = User.findByPk(appoint.userId).then(res => {
console.log('Issue', res)
})

return {
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
}
})

return res.render('appointments/list', { available })
}
}
module.exports = new MyAppointmentController()


I know this is a promise, but is impossible to get return...
The console res is print property



But user is always pending and if I try this



const user = User.findByPk(appoint.userId).then(res => {
another_var = res
return res
})


console.log(enother_var) << undefined



Why does this happen? And How I can resolve this?










share|improve this question

























  • So you are trying to get the logged in user appointments and then modify how the date looks using moment.js, correct ? why not make use of Sequelize associations and associate the Appointment model to the User using hasMany so you can just then find the user's appointments easier using an include clause or a getter ?

    – Mohdule
    Nov 29 '18 at 1:12


















0















Hello I try use Sequelize and I need return a Object in DB, this is a piece of my code;



Simple like that:



const { User, Appointment } = require('../models')
const moment = require('moment')
const { Op } = require('sequelize')

class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = appoitments.map(appoint => {
const user = User.findByPk(appoint.userId).then(res => {
console.log('Issue', res)
})

return {
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
}
})

return res.render('appointments/list', { available })
}
}
module.exports = new MyAppointmentController()


I know this is a promise, but is impossible to get return...
The console res is print property



But user is always pending and if I try this



const user = User.findByPk(appoint.userId).then(res => {
another_var = res
return res
})


console.log(enother_var) << undefined



Why does this happen? And How I can resolve this?










share|improve this question

























  • So you are trying to get the logged in user appointments and then modify how the date looks using moment.js, correct ? why not make use of Sequelize associations and associate the Appointment model to the User using hasMany so you can just then find the user's appointments easier using an include clause or a getter ?

    – Mohdule
    Nov 29 '18 at 1:12
















0












0








0








Hello I try use Sequelize and I need return a Object in DB, this is a piece of my code;



Simple like that:



const { User, Appointment } = require('../models')
const moment = require('moment')
const { Op } = require('sequelize')

class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = appoitments.map(appoint => {
const user = User.findByPk(appoint.userId).then(res => {
console.log('Issue', res)
})

return {
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
}
})

return res.render('appointments/list', { available })
}
}
module.exports = new MyAppointmentController()


I know this is a promise, but is impossible to get return...
The console res is print property



But user is always pending and if I try this



const user = User.findByPk(appoint.userId).then(res => {
another_var = res
return res
})


console.log(enother_var) << undefined



Why does this happen? And How I can resolve this?










share|improve this question
















Hello I try use Sequelize and I need return a Object in DB, this is a piece of my code;



Simple like that:



const { User, Appointment } = require('../models')
const moment = require('moment')
const { Op } = require('sequelize')

class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = appoitments.map(appoint => {
const user = User.findByPk(appoint.userId).then(res => {
console.log('Issue', res)
})

return {
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
}
})

return res.render('appointments/list', { available })
}
}
module.exports = new MyAppointmentController()


I know this is a promise, but is impossible to get return...
The console res is print property



But user is always pending and if I try this



const user = User.findByPk(appoint.userId).then(res => {
another_var = res
return res
})


console.log(enother_var) << undefined



Why does this happen? And How I can resolve this?







node.js sequelize.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 22:48







José Luiz

















asked Nov 28 '18 at 22:13









José LuizJosé Luiz

66110




66110













  • So you are trying to get the logged in user appointments and then modify how the date looks using moment.js, correct ? why not make use of Sequelize associations and associate the Appointment model to the User using hasMany so you can just then find the user's appointments easier using an include clause or a getter ?

    – Mohdule
    Nov 29 '18 at 1:12





















  • So you are trying to get the logged in user appointments and then modify how the date looks using moment.js, correct ? why not make use of Sequelize associations and associate the Appointment model to the User using hasMany so you can just then find the user's appointments easier using an include clause or a getter ?

    – Mohdule
    Nov 29 '18 at 1:12



















So you are trying to get the logged in user appointments and then modify how the date looks using moment.js, correct ? why not make use of Sequelize associations and associate the Appointment model to the User using hasMany so you can just then find the user's appointments easier using an include clause or a getter ?

– Mohdule
Nov 29 '18 at 1:12







So you are trying to get the logged in user appointments and then modify how the date looks using moment.js, correct ? why not make use of Sequelize associations and associate the Appointment model to the User using hasMany so you can just then find the user's appointments easier using an include clause or a getter ?

– Mohdule
Nov 29 '18 at 1:12














1 Answer
1






active

oldest

votes


















1














If you want to loop through a list but require an async call for each, I would recommend using async/await:



class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = ;

for (appoint of appoitments) {
const user = await User.findByPk(appoint.userId);

available.push({
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
})
};

return res.render('appointments/list', { available })
}
}


Here's why your code didn't work:




  • You were calling .map on appoitments but never returning anything. You must return a value when calling .map. In your case, you presumably intended to return a list of user instances;

  • In your loop, you assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending.

  • If you are calling an asyncronous method inside a loop, you must wait for that method to finish.






share|improve this answer


























  • * my .map have a result... I just omitted in this post... In My loop, I assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending, So how get the result from a promisse ?

    – José Luiz
    Nov 28 '18 at 22:39













  • Have you tried my suggestion?

    – mcranston18
    Nov 28 '18 at 22:42











  • async and wait is required ? but other tutorial on web does have this keyword... Why in this case I need?

    – José Luiz
    Nov 28 '18 at 22:43











  • I'm implementing your suggestion righ now...

    – José Luiz
    Nov 28 '18 at 22:43











  • async/await is not required but it will make your code a lot cleaner. Assuming you can write ES6+, I would recommend it.

    – mcranston18
    Nov 28 '18 at 22:44












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528908%2fusing-sequelize-with-nodejs-and-findbypk-return-pending%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you want to loop through a list but require an async call for each, I would recommend using async/await:



class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = ;

for (appoint of appoitments) {
const user = await User.findByPk(appoint.userId);

available.push({
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
})
};

return res.render('appointments/list', { available })
}
}


Here's why your code didn't work:




  • You were calling .map on appoitments but never returning anything. You must return a value when calling .map. In your case, you presumably intended to return a list of user instances;

  • In your loop, you assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending.

  • If you are calling an asyncronous method inside a loop, you must wait for that method to finish.






share|improve this answer


























  • * my .map have a result... I just omitted in this post... In My loop, I assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending, So how get the result from a promisse ?

    – José Luiz
    Nov 28 '18 at 22:39













  • Have you tried my suggestion?

    – mcranston18
    Nov 28 '18 at 22:42











  • async and wait is required ? but other tutorial on web does have this keyword... Why in this case I need?

    – José Luiz
    Nov 28 '18 at 22:43











  • I'm implementing your suggestion righ now...

    – José Luiz
    Nov 28 '18 at 22:43











  • async/await is not required but it will make your code a lot cleaner. Assuming you can write ES6+, I would recommend it.

    – mcranston18
    Nov 28 '18 at 22:44
















1














If you want to loop through a list but require an async call for each, I would recommend using async/await:



class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = ;

for (appoint of appoitments) {
const user = await User.findByPk(appoint.userId);

available.push({
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
})
};

return res.render('appointments/list', { available })
}
}


Here's why your code didn't work:




  • You were calling .map on appoitments but never returning anything. You must return a value when calling .map. In your case, you presumably intended to return a list of user instances;

  • In your loop, you assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending.

  • If you are calling an asyncronous method inside a loop, you must wait for that method to finish.






share|improve this answer


























  • * my .map have a result... I just omitted in this post... In My loop, I assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending, So how get the result from a promisse ?

    – José Luiz
    Nov 28 '18 at 22:39













  • Have you tried my suggestion?

    – mcranston18
    Nov 28 '18 at 22:42











  • async and wait is required ? but other tutorial on web does have this keyword... Why in this case I need?

    – José Luiz
    Nov 28 '18 at 22:43











  • I'm implementing your suggestion righ now...

    – José Luiz
    Nov 28 '18 at 22:43











  • async/await is not required but it will make your code a lot cleaner. Assuming you can write ES6+, I would recommend it.

    – mcranston18
    Nov 28 '18 at 22:44














1












1








1







If you want to loop through a list but require an async call for each, I would recommend using async/await:



class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = ;

for (appoint of appoitments) {
const user = await User.findByPk(appoint.userId);

available.push({
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
})
};

return res.render('appointments/list', { available })
}
}


Here's why your code didn't work:




  • You were calling .map on appoitments but never returning anything. You must return a value when calling .map. In your case, you presumably intended to return a list of user instances;

  • In your loop, you assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending.

  • If you are calling an asyncronous method inside a loop, you must wait for that method to finish.






share|improve this answer















If you want to loop through a list but require an async call for each, I would recommend using async/await:



class MyAppointmentController {
async index(req, res) {
const { id } = req.session.user

const appoitments = await Appointment.findAll({
where: {
providerId: id,
date: {
[Op.between]: [
moment()
.startOf('day')
.format(),
moment()
.endOf('day')
.format()
]
}
}
})

const available = ;

for (appoint of appoitments) {
const user = await User.findByPk(appoint.userId);

available.push({
appoint,
date: moment(appoint.date).format('HH:mm'),
user: user.name,
avatar: user.avatar
})
};

return res.render('appointments/list', { available })
}
}


Here's why your code didn't work:




  • You were calling .map on appoitments but never returning anything. You must return a value when calling .map. In your case, you presumably intended to return a list of user instances;

  • In your loop, you assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending.

  • If you are calling an asyncronous method inside a loop, you must wait for that method to finish.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 22:53

























answered Nov 28 '18 at 22:32









mcranston18mcranston18

2,06521925




2,06521925













  • * my .map have a result... I just omitted in this post... In My loop, I assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending, So how get the result from a promisse ?

    – José Luiz
    Nov 28 '18 at 22:39













  • Have you tried my suggestion?

    – mcranston18
    Nov 28 '18 at 22:42











  • async and wait is required ? but other tutorial on web does have this keyword... Why in this case I need?

    – José Luiz
    Nov 28 '18 at 22:43











  • I'm implementing your suggestion righ now...

    – José Luiz
    Nov 28 '18 at 22:43











  • async/await is not required but it will make your code a lot cleaner. Assuming you can write ES6+, I would recommend it.

    – mcranston18
    Nov 28 '18 at 22:44



















  • * my .map have a result... I just omitted in this post... In My loop, I assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending, So how get the result from a promisse ?

    – José Luiz
    Nov 28 '18 at 22:39













  • Have you tried my suggestion?

    – mcranston18
    Nov 28 '18 at 22:42











  • async and wait is required ? but other tutorial on web does have this keyword... Why in this case I need?

    – José Luiz
    Nov 28 '18 at 22:43











  • I'm implementing your suggestion righ now...

    – José Luiz
    Nov 28 '18 at 22:43











  • async/await is not required but it will make your code a lot cleaner. Assuming you can write ES6+, I would recommend it.

    – mcranston18
    Nov 28 '18 at 22:44

















* my .map have a result... I just omitted in this post... In My loop, I assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending, So how get the result from a promisse ?

– José Luiz
Nov 28 '18 at 22:39







* my .map have a result... I just omitted in this post... In My loop, I assigned user to a promise, as opposed to the result of the promise. That is why it always read as pending, So how get the result from a promisse ?

– José Luiz
Nov 28 '18 at 22:39















Have you tried my suggestion?

– mcranston18
Nov 28 '18 at 22:42





Have you tried my suggestion?

– mcranston18
Nov 28 '18 at 22:42













async and wait is required ? but other tutorial on web does have this keyword... Why in this case I need?

– José Luiz
Nov 28 '18 at 22:43





async and wait is required ? but other tutorial on web does have this keyword... Why in this case I need?

– José Luiz
Nov 28 '18 at 22:43













I'm implementing your suggestion righ now...

– José Luiz
Nov 28 '18 at 22:43





I'm implementing your suggestion righ now...

– José Luiz
Nov 28 '18 at 22:43













async/await is not required but it will make your code a lot cleaner. Assuming you can write ES6+, I would recommend it.

– mcranston18
Nov 28 '18 at 22:44





async/await is not required but it will make your code a lot cleaner. Assuming you can write ES6+, I would recommend it.

– mcranston18
Nov 28 '18 at 22:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528908%2fusing-sequelize-with-nodejs-and-findbypk-return-pending%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks