gpt4 book ai didi

javascript - Promise.all() 将所有结果返回为未定义

转载 作者:行者123 更新时间:2023-12-03 05:33:41 25 4
gpt4 key购买 nike

我正在从 Mongoose 的 Model.save() 构建一系列 Promise,但由于某种原因,从 Promise.all() 返回的数据是一个未定义的数组

代码

const saveDataArray = [];
//go through results and save them to the db
Object.keys(passData.scrapeUniqueData).map((index) => {
const result = gamesExport.addGame(passData.scrapeUniqueData[index], passData.leagueInfo.league_id, passData.leagueInfo.leagueID, passData.leagueInfo.year);
if(result)
saveDataArray.push(result);
});

//const -> saveDataArray
[Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise]

return Promise.all(saveDataArray)
.then((savedDBData) => {
console.info('savedDBData', savedDBData);
console.info('saveDataArray', saveDataArray);
passData.savedDBGames = savedDBData.filter(function(n){ return n != undefined });
return passData;
})

//const -> savedDBData
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]

所以我认为savedDBData应该返回与`saveDataArray相同的结果。为什么 Promise.all 不返回保存的数据?

添加游戏

addGame: (details, league_id, leagueID, year) => {
"use strict";
if(!details.result && details.result[0] === "P"){
console.error('result skipped for postponded match');
return;
}
if (details && details.notes !== "Postponed" && !details.postponed) {
var result = new Games();
result.fixtureID = details.fixtureID;

if (details.date.toString().indexOf('z') === -1) {
result.date = Date.parse(moment(details.date.toString(), "DD-MM-YYYY H:m").format());
} else {
result.date = details.date;
}
result.home_id = details.home_id._id;
result.away_id = details.away_id._id;
result.league_id = league_id;
result.leagueID = leagueID;
result.year = year;
result.norsemen = details.home_id.name.isNorsemen() || details.away_id.name.isNorsemen();

if (details.result) {
if (details.result.home === "A" && details.result.away === "A") {
result.result = ['A', 'A'];
result.postponed = true;
} else if (details.result.home === "H" && details.result.away === "W") {
result.result = ['H', 'W'];
} else if (details.result.home === "A" && details.result.away === "W") {
result.result = ['A', 'W'];
} else if (details.result.home === "C" && details.result.away === "C") {
result.result = ['C', 'C'];
result.cancelled = true;
} else if (details.result.home === "P" && details.result.away === "P") {
result.result = ['P', 'P'];
result.postponed = true;
} else {
result.result = [parseInt(details.result.home), parseInt(details.result.away, 10)];
}
}
return result.save(function (err, saveData) {
if (err) console.error(err);
return result;
});
} else {
return null;
}
},

最佳答案

当您将回调函数传递给 Mongoose 方法时,它假定您也不会使用它返回的 Promise。

我不确定这是否是设计使然(我猜是这样,因为否则常规回调和 promise 处理程序都会被调用,并产生相同的结果),但我可以使用如下代码重现您的问题这个:

let doc = new Model(...);
doc.save((err, res) => {
console.log('CB RES', res);
}).then(res => {
console.log('PR RES', res);
});

PR RES(“ promise 结果”)记录的结果为未定义,但CB RES (“回调结果”)记录正确的文档。当您不将回调函数传递给 save() 时,PR RES 会记录正确的文档。

所以你应该重写 addGame 以仅返回一个 promise ,而不是传入回调函数:

return result.save().catch(function(err) {
console.error(err);
// XXX: be aware that if you don't do anything else here,
// the error will _just_ be logged, but not propagated.
// This is also what your original code does, so I assume
// it's intentional.
});

关于javascript - Promise.all() 将所有结果返回为未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40829365/

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