我在函数内部返回一个承诺,并且在函数之外处理承诺但我得到的不能调用方法'然后'在未定义的错误。有人可以指出我做错了什么或者如何解决这个问题。
function getMeterReadingByMeterIdandDate(sequelize, Dials, Meter, Server, meter_id, date){
getNameByMeterId(sequelize, Meter, meter_id)
.then(function(meter){
getServerIdByMeterId(sequelize, Server, meter_id)
.then(function(server){
var name = meter[0].meter_id;
var channel = "Ch"+name.substr(-1);
var serialNumber = parseInt(name.substr(0,12));
console.log({name:name,channel:channel,serialNumber:serialNumber});
var server_id = server[0].server_id;
return sequelize.query("select SUM(?) as total from dial?s where RealReadDate <= ? and SN = ?",
{
model:Dials[server_id],
replacements:[channel, server_id, date.toISOString(), serialNumber],
type: sequelize.QueryTypes.SELECT
});
});
});
}
function getStartMeterReadingByMeterIdAndBillingId(sequelize, Dials, Billings, Meter, Server, meter_id, billing_id){
getStartDateByBillingId(sequelize,Billings,billing_id)
.then(function(date){
return getMeterReadingByMeterIdandDate(sequelize, Dials, Meter, Server, meter_id, date[0].start_date);
});
}
getStartMeterReadingByMeterIdAndBillingId(sequelize, Dials, Billings, Meter, Servers, meter.id, billing_id)
.then(function(current_date_result){
meter_obj.current = current_date_result[0].total;
});
我无法调用方法&#39;然后&#39;我运行callgetStartMeterReadingByMeterIdAndBillingId
时出错。我们是否允许返回Sequelize承诺,然后通过使用该函数处理其他地方。
答案 0 :(得分:0)
getStartMeterReadingByMeterIdAndBillingId
函数不会根据您的代码示例返回承诺。
假设getStartDateByBillingId
返回一个承诺,您应该将代码更改为:
function getStartMeterReadingByMeterIdAndBillingId(sequelize, Dials, Billings, Meter, Server, meter_id, billing_id){
return getStartDateByBillingId(sequelize,Billings,billing_id)
.then(function(date){
return getMeterReadingByMeterIdandDate(sequelize, Dials, Meter, Server, meter_id, date[0].start_date);
});
}