gpt4 book ai didi

node.js sails.js waterline bluebird .then 和 .spread

转载 作者:搜寻专家 更新时间:2023-10-31 23:53:46 24 4
gpt4 key购买 nike

我试图弄清楚 promises 如何在 sails 中工作,并且已经成功地通过 .then 从水线查询传递数据,但无法利用 .spread。我收到一个函数未定义的错误。对如何改进第一部分代码的工作有什么建议吗?

 //results in error
Promise.all([Xyz.find(), Abc.find()]).spread(function (someOtherResult, yetAnotherResult) {
console.log(someOtherResult)
}).catch(function (err) {
console.log(err);
})

以下工作但是从中提取数据会比较棘手,或者需要过长的嵌套 .then 子句:

    Promise.all([Xyz.find(), Abc.find()]).then(function (results) {
console.log(results[0][1]);
console.log(results[0].length);
})


Abc.find().then(function (foundAbcs) {
Promise.all(Xyz.find().then(function (foundXyzs) {
console.log(foundAbcs);
console.log(foundXyzs);
// additional syncranouse logic with Abc and Xyz
}))
})

最佳答案

好吧,非常简单的错误,我没有意识到我需要:

var Promise = require('bluebird');

在 sails.js .11 中的 module.exports 之前,问题已解决。

关于node.js sails.js waterline bluebird .then 和 .spread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com