1
votes

I can't seem to get the getData() function to run on this connector I'm building. Data studio displays my Schema properly, however when I go to 'explore' the data, an error is thrown. Looking in the project executions, the 'getData' function never runs at all.

Data Studio has encountered a system error.

Sorry, we encountered an error and were unable to complete your request.

There's no debug errors shown, and I'm not sure how to continue debugging this.

Here is my code...

var cc = DataStudioApp.createCommunityConnector();


function isAdminUser(){
  return true
}



function responseToRows(requestedFields, response){
  return response.map(function(item) {
    var row = [];
    requestedFields.asArray().forEach(function(field){
      var id = field.getId()
      row.push(item[id])
    });
    console.log(row);
    return { values: row };
  });
}

function getAuthType() {
  var response = { type: 'NONE' };
  return response;
}


function getConfig(){
  var json = UrlFetchApp.fetch("<api-url>");
  var data = JSON.parse(json);
  var config = cc.getConfig();
  var tables = data.TableNames
  var configElement = config
      .newSelectSingle()
      .setId('tables')
      .setName("Choose your data source")
      .setHelpText('Choose your data source');

  for(i=0;i<tables.length;i++){
    configElement
    .addOption(config.newOptionBuilder().setLabel(tables[i]).setValue(tables[i]))
  }

  return config.build();


}

function getSchema(request){

  var fields = cc.getFields();
  var types = cc.FieldType;
  var table = request.configParams.tables;
  var data = UrlFetchApp.fetch("<api-url>"+"?name="+table);
  var itemArray = JSON.parse(data);
  var singleRow = itemArray["Items"][0];
  var keys = Object.keys(singleRow)
  for(i=0;i<keys.length;i++){   
    var nestedKeys = Object.keys(singleRow[keys[i]])
    var propName = keys[i];
    var dataType = nestedKeys[0]
    if(dataType == "S"){
      fields.newDimension()
      .setId(propName)
      .setName(propName)
      .setType(types.TEXT)
    }else if (dataType == "N"){
      fields.newMetric()
      .setId(propName)
      .setName(propName)
      .setType(types.NUMBER)
    }

  }
  console.log(fields.build());
  console.log('get schema')
  return { schema: fields.build() };

}

function getData(request){
  var fields = cc.getFields();
  console.log(fields);
  console.log('getdata running');
  // TODO: Create Schema for requested field
  var table = request.configParams.tables;
  var requestedFieldIds = request.fields.map(function(field) {
    return field.name
  });
  var requestedFields = fields.forIds(requestedFieldIds);

  // TODO: Fetch and Parse data from API
  var response = UrlFetchApp.fetch("<api-url>"+"?name="+table);

  var parsedResponse = JSON.parse(response)

  // TODO: Transform parsed data and filter for requested fields
  var rows = responseToRows(requestedFields, parsedResponse)
  return {
    schema: requestedFields.build(),
    rows: rows
  }

}




2
I had a similar problem when I was building my connector but unless we know the data structure being returned by the API, it might be difficult to assess where the data structure (more precisely, the restructuring via responseToRows) is breaking. - Sourabh Choraria
What error message do you get if you create a report and draw a table based on the data source? - Minhaz Kazi

2 Answers

0
votes

To see debug traces, you could simply log it with console.log() and take a look at your logs in the Google Apps Scripts dashboard :

https://script.google.com/home/executions

0
votes

I don't know if this is related to your problem, but in my case I was trying to use URL Parameters and getData(request) wouldn't run no matter what values I input - it ended up being that I had to create a production deployment and Publish > Deploy from Manifest and then create an actual published version (not just FROM HEAD).