2017-09-29 33 views
0

我正在构建社区连接器并且在挠挠我的头;该文件指出:getData()中未定义的configParams调用

getData()

Returns the tabular data for the given request.

Request

@param {Object} request A JavaScript object containing the data request parameters.

The request parameter contains user provided values and additional information that can be used to complete the data request. It has the following structure:

{ "configParams": object, "scriptParams": { "sampleExtraction": boolean, "lastRefresh": string }, "dateRange": { "startDate": string, "endDate": string }, "fields": [ { object(Field) } ] }

我已经正确安装getConfig()(至少,我的配置是从用户的要求),但我的getData函数没有被通过的configParams对象。这是我的代码。

function getConfig(request) { 

    var Harvest = HarvestService({ 
    token: getHarvestAuthService().getAccessToken() 
    }); 
    var accounts = Harvest.accounts.list(); 
    var options = accounts.map(function(account) { 

    return { 
     label: account.name, 
     value: account.id 
    }; 

    }); 

    var config = { 
    configParams: [ 
     { 
     type: 'SELECT_SINGLE', 
     name: 'harvestAccountId', 
     displayName: 'Harvest Account ID', 
     helpText: 'The ID of the Harvest Account to pull data from.', 
     options: options 
     } 
    ], 
    dateRangeRequired: true 
    }; 

    return config; 

} 

function getData(request) { 

    var startDate = request.dateRange.startDate; 
    var endDate = request.dateRange.endDate; 
    var accountId = request.configParams.harvestAccountId; 
    var harvestAuthService = getHarvestAuthService(); 

    var Harvest = HarvestService({ 
    token: harvestAuthService.getAccessToken(), 
    account: accountId 
    }); 

    var fieldKeys = request.fields.map(function(field) { return field.name; }); 

    var entries = Harvest.entries.list({ 
    startDate: new Date(startDate), 
    endDate: new Date(endDate) 
    }); 

    var rows = entries.map(entryToRow); 

    return { 
    schema: request.fields, 
    rows: rows, 
    cachedData: false 
    }; 

} 

当我测试/调试,我可以选择在配置步骤中的帐户,该模式是正确返回,但我得到以下情况例外,当我试图和小部件添加到报告:

Script error message: TypeError: Cannot read property "harvestAccountId" from undefined.
Script error cause: USER Script error stacktrace: getData:244

任何建议非常感谢。

回答

1

发现了问题 - 问题是,期权的价值属性是一个数字,但它必须是一个字符串:

https://developers.google.com/datastudio/connector/reference#getconfig

的情况下,任何人在这里离开这个卡在这。您的Data Studio Community Connector的配置选择选项必须包含标签和值的字符串,并且没有人会为您强制使用它们。修复是这样的:

var options = accounts.map(function(account) { 

    return { 
    label: account.name, 
    value: account.id + '' 
    }; 

}); 
0

通常,request.configParamsundefined当存在来自用户的配置没有通过配置值。

  1. 测试连接器时,您是否在harvestAccountId的下拉列表中选择了一个值?
  2. 如果您打算与其他用户共享此连接器,在用户未选择某个选项的情况下,为harvestAccountId设置默认值可能是个好主意。
  3. 您可以使用Apps Script logging查看getConfig()的响应,以确保正确的值通过选项。然后,您还可以登录request以了解getData()以更好地理解请求中传递的内容。
+0

感谢您的回答;问题是我的价值观不是字符串,他们不是在幕后强制。 –