José Luiz
José Luiz

Reputation: 333

Using Sequelize with NodeJS and findByPk return pending

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?

Upvotes: 0

Views: 14906

Answers (1)

mcranston18
mcranston18

Reputation: 4790

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.

Upvotes: 1

Related Questions