<tfoot id='4nmWR'></tfoot>

      <small id='4nmWR'></small><noframes id='4nmWR'>

      <legend id='4nmWR'><style id='4nmWR'><dir id='4nmWR'><q id='4nmWR'></q></dir></style></legend>

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

        <bdo id='4nmWR'></bdo><ul id='4nmWR'></ul>

      Sequelize 中的 Promise:如何从每个 Promise 中获取结果

      时间:2023-09-03

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

              <legend id='sKG0p'><style id='sKG0p'><dir id='sKG0p'><q id='sKG0p'></q></dir></style></legend>
                  <tbody id='sKG0p'></tbody>

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

              • <i id='sKG0p'><tr id='sKG0p'><dt id='sKG0p'><q id='sKG0p'><span id='sKG0p'><b id='sKG0p'><form id='sKG0p'><ins id='sKG0p'></ins><ul id='sKG0p'></ul><sub id='sKG0p'></sub></form><legend id='sKG0p'></legend><bdo id='sKG0p'><pre id='sKG0p'><center id='sKG0p'></center></pre></bdo></b><th id='sKG0p'></th></span></q></dt></tr></i><div id='sKG0p'><tfoot id='sKG0p'></tfoot><dl id='sKG0p'><fieldset id='sKG0p'></fieldset></dl></div>
                <tfoot id='sKG0p'></tfoot>
              • 本文介绍了Sequelize 中的 Promise:如何从每个 Promise 中获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 Sequelize >=1.7 我们可以使用承诺

                In Sequelize >=1.7 we can use promises

                你能解释一下我如何在这段代码中从每个用户那里获取值吗:

                Can you explain for me how can i get values from each user in this code:

                var User = sequelize.define("user", {
                  username: Sequelize.STRING
                })
                
                
                User
                  .sync({ force: true })
                  .then(function() { return User.create({ username: 'John' }) })
                  .then(function(john) { return User.create({ username: 'Jane' }) })
                  .then(function(jane) { return User.create({ username: 'Pete' }) })
                  .then(function(pete) {
                    console.log("we just created 3 users :)")
                    console.log("this is pete:")
                    console.log(pete.values)
                
                    // what i want:
                    console.log("this is jane:")
                    console.log(jane.values)
                
                    console.log("this is john:")
                    console.log(john.values)
                  })
                

                UPD

                所有值都需要设置与其他模型的关联.其实我需要一些这样的代码:

                All values need for set associations with other Model. Actually i need some like this code:

                User.hasMany(Group)
                Group.hasMany(User)
                
                User
                  .sync({ force: true })
                  .then(function() { return User.create({ username: 'John' }) })
                  .then(function(john) { return User.create({ username: 'Jane' }) })
                  .then(function(jane) { return User.create({ username: 'Pete' }) })
                  .then(function(pete) { return Group.findOrCreate({id: 1}) })
                  .then(function(group) {return group.setUsers([john, jane, pete])})
                  .then(function(result) { console.log(result)})
                })
                

                推荐答案

                Bluebird 方式是 收集帮助函数.

                The Bluebird way are the collection helper functions.

                如果您想并行创建它们,请使用 map:

                If you want to create them in parallel, use map:

                User.sync({ force: true })
                  .then(function() {
                    return Promise.map( ['John', 'Jane', 'Pete'], function(name) {
                      return User.create({ username: name });
                    })
                  }).spread(function(john, jane, pete) {
                    console.log("we just created 3 users :)")
                    console.log("this is john:")
                    console.log(john.values)
                    console.log("this is jane:")
                    console.log(jane.values)
                    console.log("this is pete:")
                    console.log(pete.values)
                  })
                

                如果您需要连续创建它们,只需将其更改为 mapSeries (3.0+).

                If you need to create them consecutively, just change it to mapSeries (3.0+).

                如果数组不需要是动态的,并且您只想像示例中那样通过承诺链传递共享值,请查看 如何在 .then() 链中访问之前的承诺结果?.

                If the array doesn't need to be dynamic, and you simply want to pass a shared value through the promise chain like in your example, have a look at How do I access previous promise results in a .then() chain?.

                这篇关于Sequelize 中的 Promise:如何从每个 Promise 中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:复数如何在 Sequelize 中工作? 下一篇:同步执行 Sequelize 查询

                相关文章

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

              • <legend id='VeMhX'><style id='VeMhX'><dir id='VeMhX'><q id='VeMhX'></q></dir></style></legend>

                  <tfoot id='VeMhX'></tfoot>
                1. <small id='VeMhX'></small><noframes id='VeMhX'>