我嘗試使用$ http.post中的承諾響應來獲取記錄的新ID作爲整數並將其用於下一個$ http.post。我用How do I return the response from an asynchronous call?尋求幫助,但它仍然沒有工作。
var streckenId;
var trackpunktId;
var getStreckeID = function() {
$http.post(dbStrecke, {"ort": location, "distanz": km}).then(function (response) {
streckenId = response.data;
console.log("ID (then): " + streckenId); // return ID
});
return streckenId;
};
streckenId = getStreckeID();
console.log("StreckeID:" + streckenId); //return undefined
最後一個console.log仍然說「streckenId」未定義。
你沒有很好地閱讀這個問題的答案,你仍然在嘗試使用尚未設置的東西。 –
你在開玩笑嗎?我試圖用上面的線程解決,但它不工作,所以我仍然沒有幫助?偉大的社區! – J4ck
它確實有效,你只是錯誤地實現了它。 –