我正在构建一系列来自Mongoose的Model.save()
的承诺但是由于某种原因,来自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]
所以imo savedDBData
应该和`saveDataArray一样返回。为什么没有Promise.all返回保存的数据?
AddGame
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;
}
},
答案 0 :(得分:1)
当您将回调函数传递给Mongoose方法时,它会假定您不会使用它返回的承诺。
我不确定这是否是设计的(我猜是这样,因为否则会调用常规回调和promise处理程序,结果相同),但我可以使用代码重现您的问题看起来像这样:
let doc = new Model(...);
doc.save((err, res) => {
console.log('CB RES', res);
}).then(res => {
console.log('PR RES', res);
});
PR RES
("承诺结果" )的记录结果为undefined
,但CB RES
(&# 34;回调结果" )记录正确的文件。如果您未将回调函数传递给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.
});