[英]Filtering data in custom connector for DataStudio
I'm trying to create a custom datastudio
connector that connect to WooCommerce rest API. I want to differentiate between orders placed by a registered user
and orders placed by a guest user
.我正在尝试创建一个连接到 WooCommerce rest API 的自定义
datastudio
连接器。我想区分registered user
下的订单和guest user
下的订单。
The WooCommerce API gives me the custumer_id
field, if the customer_id = 0
, the order was placed by a guest
, otherwise the user is registered
. WooCommerce API 给我
custumer_id
字段,如果customer_id = 0
,订单是由guest
下的,否则user is registered
。
I followed the google data studio tutorial: https://developers.google.com/datastudio/connector/get-started我遵循了谷歌数据工作室教程: https://developers.google.com/datastudio/connector/get-started
And this is my responseToRow
function:这是我的
responseToRow
function:
/**
Parse the response data given the required fields of the config
@return the parsed data
*/
function responseToRows(requestedFields, response) {
// Transform parsed data and filter for requested fields
return response.map(function(dailyDownload) {
var row = [];
requestedFields.asArray().forEach(function (field) {
switch (field.getId()) {
case 'id_order':
return row.push(dailyDownload.id);
case 'total':
return row.push(dailyDownload.total);
case 'date_created':
return row.push(dailyDownload.date_created);
case 'registered_user' :
if(parseInt(dailyDownload.customer_id) > 0)
return row.push(dailyDownload.customer_id);
case 'guest_user' :
if(parseInt(dailyDownload.customer_id) == 0)
return row.push(dailyDownload.customer_id);
default:
return row.push('');
}
});
return { values: row };
});
}
The function is similar to the one given in the tutorial, the others fields works fine. function 与教程中给出的类似,其他字段工作正常。 I'm just returning when the customer_id is different of 0. It seem to work, but I get null values when the condition doesn't hold.
当 customer_id 不同于 0 时,我才返回。它似乎有效,但当条件不成立时,我得到 null 值。
I would like to remove the null values, having only the orders when the customer_id
was 0 on the right and the same for the complement on the left part.我想删除 null 值,只有当
customer_id
为 0 时右侧的订单和左侧补码相同的订单。
Thanks for the help谢谢您的帮助
I would try it like that after filling the row array:我会在填充行数组后尝试这样做:
row.forEach((element, index) => {
if(element.guest_user == null){
// To remove the row-entry with guest_user==null
row[index].slice();
}
})
And if you don't want to remove the entries from the array just create a new array and use newArray.push(element);如果您不想从数组中删除条目,只需创建一个新数组并使用 newArray.push(element); to copy the null ones into it.
将 null 复制到其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.