社区连接器“ response”和“ requestedFields”未定义

时间:2019-08-19 22:18:38

标签: api google-apps-script google-data-studio

我正在构建从Google Data Studio到SpyFu API的社区连接器。

我已经将我的代码与“社区连接器教程”进行了比较,看起来不错,但是即使在进行API调用之后,关键变量仍未定义。

直到我开始运行实时API请求(从清单部署)并使用正确的API请求格式后,才会弹出此错误。为了澄清,当我尝试将API数据添加到我的Google Data Studio中时,会弹出此错误。

错误:TypeError:无法调用未定义的方法“ map”。 显然:“ response”和“ requestedFields”保持未定义状态。我不知道为什么。

from script.google.com

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

function getConfig(request) {
  var cc = DataStudioApp.createCommunityConnector();
  var config = cc.getConfig();

  config.newInfo()
    .setId('instructions')
    .setText('Give me SpyFu information on the following domain:');

  config.newTextInput()
    .setId('domain')
    .setName('Enter the domain to search')
    .setHelpText('e.g. ebay.com')
    .setPlaceholder('ebay.com');

    config.newTextInput()
    .setId('SECRET_KEY')
    .setName('Enter your API Secret Key')
    .setHelpText('e.g. A1B2C3D4')
    .setPlaceholder('A1B2C3D4');

  //config.setDateRangeRequired(true);

  return config.build();
}

function getFields(request) {
  var cc = DataStudioApp.createCommunityConnector();
  var fields = cc.getFields();
  var types = cc.FieldType;

  fields.newDimension()
    .setId('Keyword')
    .setName('Keywords')
    .setDescription('The keywords most often attributed to this domain.')
    .setType(types.TEXT);

  fields.newMetric()
    .setId('Rank')
    .setName('Rankings')
    .setDescription('The ranking of the target site keyword on the Google Search Page.')
    .setType(types.NUMBER);

  fields.newMetric()
    .setId('Monthly_Searches')
    .setName('Searches per Month')
    .setDescription('Number of times people have searched for this term within in the last month.')
    .setType(types.NUMBER);

  return fields;
}

function getSchema(request) {
  var fields = getFields(request).build();
  return { schema: fields };
}

function responseToRows(requestedFields, response) {
  return response.map(function(Array) {
    var row = [];
    requestedFields.asArray().forEach(function (field) {
      switch (field.getId()) {
        case 'Keyword':
          return row.push(Array.term);
        case 'Rank':
          return row.push(Array.position);
        case 'Monthly_Searches':
          return row.push(Array.exact_global_monthly_search_volume);
        default:
          return row.push('');
      }
    });
    return { values: row };
  });
}

function getData(request) {
  var requestedFieldIds = request.fields.map(function(field) {
    return field.name;
  });
  var requestedFields = getFields().forIds(requestedFieldIds);

  // Fetch data from API  
  var url = [
    'https://www.spyfu.com/apis/url_api/organic_kws?q=' 
    + request.configParams.domain
    + '&r=10'
    + '&api_key='
    + request.configParams.SECRET_KEY
  ];

try {   
  var response = UrlFetchApp.fetch(url.join(''));
} catch (e) {
  DataStudioApp.createCommunityConnector()  
    .newUserError()
    .setDebugText('Failed URL Fetch Attempt. Exception details: ' + e)
    .setText('There was an error accessing this domain. Try again later, or file an issue if this error persists.')
    .throwException();
  } 

try { 
  var parsedResponse = JSON.parse(response).downloads;
} catch (e) {
  DataStudioApp.createCommunityConnector()  
    .newUserError()
    .setDebugText('Error parsing the JSON data. Exception details: ' + e)
    .setText('There was an error parsing the JSON data. Try again later, or file an issue if this error persists.')
    .throwException();
  }

  var rows = responseToRows(requestedFields, parsedResponse);
  return {
    schema: requestedFields.build(),
    rows: rows
  };
}

我尝试过更改要映射到的变量,但是在这里没有任何进展。任何帮助将不胜感激。

0 个答案:

没有答案