2016-02-29 147 views
0

我試圖創建自定義Web數據連接器,我使用Tableau Web數據連接器教程中給出的示例。Tableau自定義Web數據連接器

我有一個鏈接,它以JSON的形式返回我的數據,並且我在jQuery AJAX函數中傳遞了這個URL,並試圖對console.log結果進行處理。

但不幸的是,我收到了一個錯誤,如下所述。

Uncaught WDC error: Uncaught TypeError: Cannot read property 'results' of undefined stack:TypeError: Cannot read property 'results' of undefined 

當我用jQuery一個AJAX請求不使用畫面網絡數據連接器的js文件,我能夠從我們創建的鏈接檢索數據。

當我比較yahooapi的結果和我們創建的鏈接時,數據就是這種格式。

雅虎API:

Object{*query*: Object} 

從鏈接我們創建了:

[Object, Object, Object, Object] 

這會不會有什麼區別?

請幫我解決這個問題。

+0

你能張貼能重現該問題的代碼的樣本?愛會幫助調試。 – lbrendanl

+0

@lbrendanl - 能夠連接並獲取數據。 –

回答

0

我意識到這是一箇舊帖子,我不確定是否有人仍然需要這個問題的幫助,但我發佈了自己的問題後發現它。過去一年中,我多次使用此REDCap WDC來創建自定義Tableau WDC。我知道這個例子專門用於連接REDCap數據,但是當我開始構建自定義的WDC時,WDC的格式/結構就是我所說的最多的。看看代碼,你會發現它很有用。

(function() { 
 
     var myConnector = tableau.makeConnector(); 
 
     // Define the schema 
 
     // myConnector.getSchema = function(schemaCallback){} 
 
     myConnector.getSchema = function(schemaCallback) { 
 
      var recordsInfo = []; 
 
      $.ajax({ 
 
      url: JSON.parse(tableau.connectionData)['url'], 
 
      type: "POST", 
 
      data: { 
 
       token: JSON.parse(tableau.connectionData)['token'], 
 
       content: 'exportFieldNames', 
 
       format: 'json', 
 
       returnFormat: 'json', 
 
       type: 'flat', 
 
       rawOrLabelHeaders: 'raw', 
 
       exportCheckboxLabel: 'true', 
 
       exportSurveyFields: 'true', 
 
       exportDataAccessGroups: 'true' 
 
       }, 
 
      contentType: "application/x-www-form-urlencoded", 
 
      dataType: "json", 
 
      success: function(resp){ 
 
       recordsInfo = resp; 
 
       var recordSchema = []; 
 
       recordsInfo.forEach(function(field){ 
 
        recordSchema.push({ 
 
        id: field.export_field_name, 
 
        alias: field.original_field_name, 
 
        dataType: tableau.dataTypeEnum.string 
 
        }); 
 
       }); 
 
       var redcapTable = { 
 
        id: "redcap", 
 
        alias: "custom redcap extract", 
 
        columns: recordSchema 
 
       } 
 
       schemaCallback([redcapTable]); 
 
       } 
 
      }); 
 
     }; 
 
     // Download the data 
 
     myConnector.getData = function(table, doneCallback) { 
 
     var tableData = []; 
 
      $.ajax({ 
 
      url: JSON.parse(tableau.connectionData)['url'], 
 
      type: "POST", 
 
      data: { 
 
       token: JSON.parse(tableau.connectionData)['token'], 
 
       content: 'record', 
 
       format: 'json', 
 
       returnFormat: 'json', 
 
       type: 'flat', 
 
       rawOrLabelHeaders: 'raw', 
 
       exportCheckboxLabel: 'true', 
 
       exportSurveyFields: 'true', 
 
       exportDataAccessGroups: 'true' 
 
      }, 
 
      contentType: "application/x-www-form-urlencoded", 
 
      dataType: "json", 
 
      success: function(resp){ 
 
      resp.forEach(function(record){ 
 
       tableData.push(record); 
 
      }); 
 
      table.appendRows(tableData); 
 
      doneCallback(); 
 
      } 
 
     }); 
 
     } 
 
     tableau.registerConnector(myConnector); 
 
     $(document).ready(function(){ 
 
     $("#submitButton").click(function() { 
 
      tableau.connectionData = JSON.stringify({ 
 
       'token': $("#token").val(), 
 
       'url': $("#url").val() 
 
      }); 
 
      tableau.connectionName = "REDCap Data"; 
 
      tableau.submit(); 
 
     }); 
 
     }); 
 
    })();

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/17793320) –

+0

我在Github項目中添加了一個代碼片段。 – Dklaver