sql >> Database teknologi >  >> RDS >> PostgreSQL

Udfør Sequelize-forespørgsler synkront

Brug Promise.all for at udføre alle dine forespørgsler, skal du kalde den næste funktion.

models.Attendance.findAll({
    where: {
        UserId: req.user.id
    }
}).then(function (data) {
    // get an array of the data keys, (not sure if you need to do this)
    // it is unclear whether data is an object of users or an array. I assume
    // it's an object as you used a `for in` loop
    const keys = Object.keys(data)
    // map the data keys to [Promise(query), Promise(query), {...}]
    const hacks = keys.map((d) => {
      return models.Hackathon.findOne({
        where: {
          id: data[d].id
        }
      })
    })
    // user Promise.all to resolve all of the promises asynchronously
    Promise.all(hacks)
      // this will be called once all promises have resolved so
      // you can modify your data. it will be an array of the returned values
      .then((users) => {
        const [user1, user2, {...}] = users
        res.render('dashboard/index.ejs', {
          title: 'My Hackathons', 
          user: req.user, 
          hacks: users
        });
      })
});


  1. Afkobling af MySQL-data versus brugervenlighed

  2. De 8 mest populære databaser

  3. Looping svg rektangler fra databasen

  4. MySQL Contiguous Sequential Rows Field selv ved sletning og indsættelse