[英]Google Data Studio - Custom Connector - Get Data parsing json
I'm trying to build a custom connector on GDS for an external API.我正在尝试在 GDS 上为外部 API 构建自定义连接器。
I've configured the schema with all the fields coming from the API and I can see that correctly when I deploy and try the connector.我已经使用来自 API 的所有字段配置了架构,当我部署并尝试连接器时,我可以正确地看到它。 However when I try to run the "Explore" I get a generic "Data Set Configuration Error - Data Studio cannot connect to your data set".
但是,当我尝试运行“Explore”时,我得到一个通用的“数据集配置错误 - Data Studio 无法连接到您的数据集”。 I am passing an array of key value pairs as requested... so not sure what's going on.
我正在按要求传递一组键值对……所以不确定发生了什么。
This is the code I am using in the getData function这是我在 getData function 中使用的代码
function getData(request) {
try {
request.configParams = validateConfig(request.configParams);
var requestedFields = getFields().forIds(
request.fields.map(function(field) {
return field.name;
})
);
var data = JSON.parse(jsonSample).Table
return {
schema: requestedFields.build(),
rows: data
};
}
catch (e) {
cc.newUserError()
.setDebugText('Error fetching data from API. Exception details: ' + e)
.setText(
'The connector has encountered an unrecoverable error. Please try again later, or file an issue if this error persists.'
)
.throwException();
}
}
Where jsonSample is a text string contain the following json (raw, not beautified):其中 jsonSample 是一个文本字符串,包含以下 json(原始,未美化):
{
"Table": [
{
"Entity": "Houston Heights",
"EntityD": "",
"Consolidation": "USD",
"ConsolidationD": "United States of America, Dollars",
"Scenario": "Actual",
"ScenarioD": "",
"Time": "2010M1",
"TimeD": "Jan 2010",
"View": "Periodic",
"ViewD": "",
"Account": "IFRS Balance Sheet",
"AccountD": "",
"Flow": "None",
"FlowD": "",
"Origin": "BeforeAdj",
"OriginD": "",
"IC": "None",
"ICD": "",
"UD1": "None",
"UD1D": "",
"UD2": "None",
"UD2D": "",
"UD3": "None",
"UD3D": "",
"UD4": "None",
"UD4D": "",
"UD5": "None",
"UD5D": "",
"UD6": "None",
"UD6D": "",
"UD7": "None",
"UD7D": "",
"UD8": "None",
"UD8D": "",
"CellValue": 2.25000000000000000000
},
{
"Entity": "Houston Heights",
"EntityD": "",
"Consolidation": "USD",
"ConsolidationD": "United States of America, Dollars",
"Scenario": "Actual",
"ScenarioD": "",
"Time": "2010M1",
"TimeD": "Jan 2010",
"View": "Periodic",
"ViewD": "",
"Account": "IFRS Balance Sheet",
"AccountD": "",
"Flow": "None",
"FlowD": "",
"Origin": "BeforeAdj",
"OriginD": "",
"IC": "None",
"ICD": "",
"UD1": "Admin",
"UD1D": "Admin",
"UD2": "None",
"UD2D": "",
"UD3": "None",
"UD3D": "",
"UD4": "None",
"UD4D": "",
"UD5": "None",
"UD5D": "",
"UD6": "None",
"UD6D": "",
"UD7": "None",
"UD7D": "",
"UD8": "None",
"UD8D": "",
"CellValue": 2.240000000000000000000
}
]
}
To solve this issue, the code should be like this because you are passing an array of objects:要解决这个问题,代码应该是这样的,因为你正在传递一个对象数组:
function getData(request) {
try {
request.configParams = validateConfig(request.configParams);
var requestedFields = getFields().forIds(
request.fields.map(function (field) {
return field.name;
})
);
var data = JSON.parse(jsonSample).Table;
return {
schema: requestedFields.build(),
rows: data.map(function (row) {
var values = [];
requestedFields.asArray().forEach(function (field) {
values.push(row[field.getId()]);
});
return { values: values };
}),
};
} catch (e) {
cc.newUserError()
.setDebugText("Error fetching data from API. Exception details: " + e)
.setText(
"The connector has encountered an unrecoverable error. Please try again later, or file an issue if this error persists."
)
.throwException();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.