<small id='8SNpQ'></small><noframes id='8SNpQ'>

  • <legend id='8SNpQ'><style id='8SNpQ'><dir id='8SNpQ'><q id='8SNpQ'></q></dir></style></legend>

      <i id='8SNpQ'><tr id='8SNpQ'><dt id='8SNpQ'><q id='8SNpQ'><span id='8SNpQ'><b id='8SNpQ'><form id='8SNpQ'><ins id='8SNpQ'></ins><ul id='8SNpQ'></ul><sub id='8SNpQ'></sub></form><legend id='8SNpQ'></legend><bdo id='8SNpQ'><pre id='8SNpQ'><center id='8SNpQ'></center></pre></bdo></b><th id='8SNpQ'></th></span></q></dt></tr></i><div id='8SNpQ'><tfoot id='8SNpQ'></tfoot><dl id='8SNpQ'><fieldset id='8SNpQ'></fieldset></dl></div>
      • <bdo id='8SNpQ'></bdo><ul id='8SNpQ'></ul>
    1. <tfoot id='8SNpQ'></tfoot>

        同步执行 Sequelize 查询

        时间:2023-09-03

        <small id='p3D18'></small><noframes id='p3D18'>

            <bdo id='p3D18'></bdo><ul id='p3D18'></ul>
            <legend id='p3D18'><style id='p3D18'><dir id='p3D18'><q id='p3D18'></q></dir></style></legend>
            <tfoot id='p3D18'></tfoot>

            <i id='p3D18'><tr id='p3D18'><dt id='p3D18'><q id='p3D18'><span id='p3D18'><b id='p3D18'><form id='p3D18'><ins id='p3D18'></ins><ul id='p3D18'></ul><sub id='p3D18'></sub></form><legend id='p3D18'></legend><bdo id='p3D18'><pre id='p3D18'><center id='p3D18'></center></pre></bdo></b><th id='p3D18'></th></span></q></dt></tr></i><div id='p3D18'><tfoot id='p3D18'></tfoot><dl id='p3D18'><fieldset id='p3D18'></fieldset></dl></div>
                <tbody id='p3D18'></tbody>
                • 本文介绍了同步执行 Sequelize 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Node.js 和 Sequelize(带有 Postgres 后端)构建一个网站.我有一个查询,它返回许多带有外键的对象,我想将外键引用的对象列表传递给视图.

                  I am building a website using Node.js and Sequelize (with a Postgres backend). I have a query that returns many objects with a foreign key, and I want to pass to the view a list of the objects that the foreign key references.

                  在示例中,Attendances 包含 Hackathon 密钥,我想返回一个 Hackathon 列表.既然代码是异步的,那么下面的东西在Node中当然是行不通的:

                  In the example, Attendances contains Hackathon keys, and I want to return a list of hackathons. Since the code is async, the following thing of course does not work in Node:

                  models.Attendance.findAll({
                      where: {
                          UserId: req.user.id
                      }
                  }).then(function (data) {
                      var hacks = [];
                      for (var d in data) {
                          models.Hackathon.findOne({
                              where: {
                                  id: data[d].id
                              }
                          }).then(function (data1) {
                              hacks.append(data1);
                          });
                      }
                      res.render('dashboard/index.ejs', {title: 'My Hackathons', user: req.user, hacks: hacks});
                  });
                  

                  有没有办法以同步方式进行该查询,这意味着我不会返回视图,直到我将hacks"列表填满所有对象?

                  Is there any way to do that query in a synchronous way, meaning that I don't return the view untill I have the "hacks" list filled with all the objects?

                  谢谢!

                  推荐答案

                  使用 Promise.all 执行所有查询,然后调用下一个函数.

                  Use Promise.all to execute all of your queries then call the next function.

                  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
                          });
                        })
                  });

                  这篇关于同步执行 Sequelize 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Sequelize 中的 Promise:如何从每个 Promise 中获取结果 下一篇:节点续集 - “按原样"插入日期无需转换为 UTC

                  相关文章

                      <bdo id='c4xy0'></bdo><ul id='c4xy0'></ul>

                    <small id='c4xy0'></small><noframes id='c4xy0'>

                    1. <i id='c4xy0'><tr id='c4xy0'><dt id='c4xy0'><q id='c4xy0'><span id='c4xy0'><b id='c4xy0'><form id='c4xy0'><ins id='c4xy0'></ins><ul id='c4xy0'></ul><sub id='c4xy0'></sub></form><legend id='c4xy0'></legend><bdo id='c4xy0'><pre id='c4xy0'><center id='c4xy0'></center></pre></bdo></b><th id='c4xy0'></th></span></q></dt></tr></i><div id='c4xy0'><tfoot id='c4xy0'></tfoot><dl id='c4xy0'><fieldset id='c4xy0'></fieldset></dl></div>
                      <tfoot id='c4xy0'></tfoot>
                    2. <legend id='c4xy0'><style id='c4xy0'><dir id='c4xy0'><q id='c4xy0'></q></dir></style></legend>