2017-06-05 56 views
0

我在函數內部返回一個promise並且處理函數外的promise,但是我得到的不能在未定義的錯誤上調用方法'then'。有人可以指出我做錯了什麼,或者如何解決這個問題。不能調用方法'然後'未定義的sequelize錯誤

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; 
}); 

當我功能callgetStartMeterReadingByMeterIdAndBillingId時,我得到不能調用方法'然後'錯誤。我們是否被允許返回像Sequelize一樣的承諾,然後通過使用函數來處理其他地方。

回答

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); 
    }); 
} 
+0

,這仍可能得不到你想要的結果,因爲getMeterReadingByMeterIdandDate也不會返回任何東西。 – eczajk

相關問題