Reputation: 91
I'm in the process of building a community connector and am scratching my head; the documentation states:
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) } ] }
I've correctly setup getConfig() (at least, my configurations are requested from the user), but my getData function is not being passed a configParams object. Here's my code.
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
};
}
When I test/debug, I can select an Account at the config step, the schema is correctly returned, but I get the following exception when I try and add a widget to the report:
Script error message: TypeError: Cannot read property "harvestAccountId" from undefined.
Script error cause: USER Script error stacktrace: getData:244
Any advice greatly appreciated.
Upvotes: 2
Views: 2011
Reputation: 1084
Leaving this in case it helps anyone, note that the config params in the UI although they have a placeholder value need to be physically populated to appear in the request. Indeed, if none of these are filled in the configParams
value does not appear in the request
object.
Upvotes: 1
Reputation: 3168
Usually, request.configParams
is undefined
when there are no configuration values passed from the user config.
harvestAccountId
? harvestAccountId
in case the user does not select an option.getConfig()
to ensure that right values are getting passed for options. Then you can also log the request
for getData()
to have a better understanding of what exactly is getting passed in the request.Upvotes: 2
Reputation: 91
Found out the problem - the issue was that the value attribute of the option was a number, but it MUST be a string:
https://developers.google.com/datastudio/connector/reference#getconfig
Leaving this here in case anyone else gets stuck on this. Your config select options for your Data Studio Community Connector must have strings for both the label and the value, and nobody will coerce them for you. Fix was this:
var options = accounts.map(function(account) {
return {
label: account.name,
value: account.id + ''
};
});
Upvotes: 3