javascript - 在 Tableau 中使用 Promise 解析 Json

标签 javascript json tableau-api

我正在研究 Tableau,我必须构建自己的 Web 数据连接器。 我写了它,它在 Chrome 上完美运行。 但是当我在 Tableau 中使用它时,我收到了愚蠢的错误:

ReferenceError: Can't find variable: Promise file: http://localhost:9000/json-connector line: 246

我的连接器分为两部分。第一部分调用 Web 服务来获取目的地列表并用目的地名称填充两个列表。第二部分调用另一个 Web 服务来获取两个选定目的地之间的每条路径。

当我想要获取第一个列表时发生错误。我想强调它适用于 chrome,但不适用于 tableau。

发生错误的代码部分:

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

我认为tableau不支持Promise。但我不知道如何解决。非常感谢您的帮助!

这就是我使用此功能的方式:

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

最佳答案

按照 Tomalak 的建议,我在脚本中添加了一个库,并且 Tableau 能够使用 Promise。

<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.min.js" type="text/javascript"></script>

我没有对代码进行其他更改。

关于javascript - 在 Tableau 中使用 Promise 解析 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36133625/

相关文章:

excel - 对具有拼写错误、间距差异等的相似字符串进行分组

JavaScript ES2015 : Instanciate a class and use variables from class

javascript - 在 Javascript 中将 Array 对象转换为 Json

javascript - 使用 jQuery 将文件下载到本地计算机

android - 在 flutter 上解析 json 时出错。类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型转换中类型 'ResultData' 的子类型

javascript - 根据对象的另一个值将相同的多个对象插入多个数组

tableau-api - 我们可以在 tableau 仪表板上应用 .css 文件吗?

graphics - 如何将字段名称移动到 Tableau 中的图表底部?

javascript - 创建下拉页面

javascript - 无法读取未定义的 angular2 的属性