我正在使用tableau,並且必須構建自己的Web數據連接器。 我寫了它,它在Chrome上完美運行。 但是當我用它爲我的Tableau得到foolowing錯誤:在Tableau中解析帶有Promise的Json
ReferenceError: Can't find variable: Promise file: http://localhost:9000/json-connector line: 246
我的連接器兩個部分devided。第一部分調用Web服務來獲取目的地列表,並用他們的名字填充兩個列表。第二部分調用另一個Web服務來獲取兩個選定目的地之間的每條路徑。
我想要獲得第一個列表時發生錯誤。我想堅持認爲它適用於Chrome,但不適用於畫面。
的代碼中的錯誤發生的部分:
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
console.log("Something went wrong with the destination")
reject(status);
}
};
xhr.send();
});
};
我覺得畫面不支持的承諾。但我不知道如何解決問題。非常感謝你的幫助 !
這是我如何使用這個功能:
getJSON('http://localhost:9000/stations').then(function(data) {
//alert('Your Json result is: ' + data.result); //you can comment this, i used it to debug
//result.innerText = JSON.stringify(data); //display the result in an HTML element
console.log("starting parsing on : " + data.length);
var listArrival = document.getElementById('Arrival'); //get the list where you want to add options
var listDeparture = document.getElementById('Departure');
if(listArrival == null || listDeparture == null) console.error("Impossible to retrieve the list")
var op;
var addedStations = [];
for(var i = 0; i < data.length ; i++){
var obj = data[i];
var overflowControler = 0;
for(var key in obj){
console.log(key + '=' + obj[key]);
if(key == 'name' && addedStations.indexOf(obj[key]) == -1){
op = new Option(obj[key], obj['nlc'], true);
op2 = new Option(obj[key], obj['nlc'], true);
if(op == null) console.error("Impossible to create the new option")
listArrival.add(op);
listDeparture.add(op2);
addedStations.push(obj[key]);
}
overflowControler ++;
if(overflowControler > maxLengthOfEachRecordFromJson) break; // overflow control
}
if(i > maxStationsRecordNumberFromJson) break; //overflow control
}
}, function(status) { //error detection....
alert('Something went wrong.');
});
你可以在你的tableau項目中包含一個外部Promise庫嗎(如果這是正確的名稱,我不知道很多關於tableau的內容)? – Tomalak
我可以在我的js中包含一些庫yes! 例如: –
然後包含以下內容之一:https://cdnjs.com/#q=promise - d去尋找'bluebird',但如果你所需要的只是基本的ES6承諾,那麼'es6-promise'就可以做到更輕量級。 – Tomalak