admin管理员组文章数量:1330679
I'm cleaning up some sequelize
code and the findOrCreate
function returns a promise that requires spreading to get the actual result object.
I'd like to rewrite my code to use await
instead and, given ES6 supports array destructuring I'd have thought that instead of
User.findOrCreate({ where: { mcId }, defaults }).spread((user, created) => {
// do stuff
})
I'd just be able to do
const [user, created] = await User.findOrCreate({ where: { mcId }, defaults })
but alas that's not the case.
I get the error (intermediate value) is not iterable
Is there any special trick to doing this or is what I am trying to do just not possible?
I'm cleaning up some sequelize
code and the findOrCreate
function returns a promise that requires spreading to get the actual result object.
I'd like to rewrite my code to use await
instead and, given ES6 supports array destructuring I'd have thought that instead of
User.findOrCreate({ where: { mcId }, defaults }).spread((user, created) => {
// do stuff
})
I'd just be able to do
const [user, created] = await User.findOrCreate({ where: { mcId }, defaults })
but alas that's not the case.
I get the error (intermediate value) is not iterable
Is there any special trick to doing this or is what I am trying to do just not possible?
Share edited Sep 7, 2018 at 0:15 Patrick Roberts 52k10 gold badges117 silver badges163 bronze badges asked Apr 11, 2018 at 2:26 Dave SagDave Sag 13.5k14 gold badges91 silver badges139 bronze badges 4-
2
Note that .spread() implicitly does .all() but the ES6 destructuring syntax doesn't - so, perhaps
const [user, created] = await User.findOrCreate({ where: { mcId }, defaults }).all();
– Jaromanda X Commented Apr 11, 2018 at 2:34 -
weirdly if I do that I get this error
SequelizeDatabaseError: relation "Users" does not exist
— but with the oldspread
notation I get no such error. – Dave Sag Commented Apr 11, 2018 at 2:54 -
Unless you use a pre 2014 version of sequelize the
findOrCreate
should return an array and assuming it returns a promise then the spread should just work. What is result in:findOrCreate(...).then(result=>console.log(result))
– HMR Commented Apr 11, 2018 at 3:43 -
I ended up just not using
findOrCreate
. Will e back to this later if I get time. – Dave Sag Commented Apr 23, 2018 at 2:32
1 Answer
Reset to default 7I'm using Sequelize v4.37.10 and this worked for me -
const [address, created] = await models.Address.findOrCreate({ where: { pid: 123}, defaults});
本文标签: Javascript How to migrate promisespread syntax to asyncawait with destructuringStack Overflow
版权声明:本文标题:Javascript How to migrate promise.spread syntax to asyncawait with destructuring - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742261336a2442570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论