我需要一些帮助来了解Javascript承诺。我使用的是postgres,我想要一个函数,该函数将按照下面的示例
[ { airlineName: 'NZ',
{ airlineName: 'TN']
我希望它返回如下数组
[ { airlineName: { iatacode: 'NZ', airlineName: 'Air New Zealand' },
{ airlineName: { iatacode: 'TN', airlineName: 'Air Tahiti Nui' }]
为简单起见,我在这里简化了查询,但是查询工作正常,
如果我用控制台控制台记录数组,则会以上面的正确格式获取结果,但是如果我尝试返回该语句,则会得到未定义的回传!
有什么想法吗?
exports.extractAirlineName = (flightInfo) => {
db.many("SELECT airline_name FROM airline_info WHERE iatacode IN 'NZ', 'TN'")
.then(airline => {
flightInfo.forEach((flight, index) => {
flight.airlineName = {
iatacode: flight.airlineName,
airlineName: airline[index].airline_name
};
// this shows the correctly populated object
console.log(flightInfo);
//this just returns undefined
return flightInfo;
});
});
};