我一直在尝试根据我的“ note”值过滤API调用的结果。我一直在Zapier上构建它,并且该调用有效,但是我似乎找不到找到使filter函数完成其工作的方法(因此,如果我将第19-23行替换为return results
,则它将给我来自api调用)。我已经遍历了所有可以找到的堆栈文档,但是它们都以错误result.filter not found
或bargle
错误(Zapier中的一般错误)结尾。
const options = {
url: `https://mystorename.myshopify.com/admin/orders.json?`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
params: {
}
}
return z.request(options)
.then((response) => {
response.throwForStatus();
var results = z.JSON.parse(response.content);
var queryItem = "555-5555"
const filteredOrders = results.orders.filter(item => item.note === queryItem);
return filteredOrders;
});
这是我当前使用return results;
而没有过滤器的输出示例:
{
"orders": [
{
"note": "555-5555",
"subtotal_price": "1.00"
},
{
"note": "555-6666",
"subtotal_price": "2.00"
}
]
}
同样,目标是按“注释”键中的值进行过滤。因此,如果我的过滤器输入是555-5555,则它应仅返回该项目的所有信息。我确实尝试过使用if语句进行返回,字符串化而不是解析,隐式转换为数组...全部都包含所需的代码,但是无论使用哪种格式,我都发现过滤器不起作用或不返回任何内容。继续进行下去,因此,如果我碰巧找到了答案,我会发布该答案,但在这一点上,我感到束手无策。
您正在尝试在对象中使用方法filter
,但过滤器仅在数组中可用,因此您应尝试在orders数组中调用filter
。
let results = {
"orders": [
{
"note": "555-5555",
"subtotal_price": "1.00"
},
{
"note": "555-6666",
"subtotal_price": "2.00"
}
]
}
let queryItem = "555-5555";
let newArray = results.orders.filter(function (item) {
return item.note == queryItem
})
console.log(newArray)
您正在尝试对results
进行过滤,但是根据您的输出,您应该对results.orders
进行过滤。
const filteredOrders = results.orders.filter(item => item.note === queryItem);
您是否收回所有订单(所有具有指定过滤器值的订单)?
我意识到我并没有退回所有订单,这可以解决问题:
`https://mystorename.myshopify.com/admin/orders.json?status=any`
或者,您可以查询带有特定注释的订单:
`https://mystorename.myshopify.com/admin/orders.json?status=any?note=` + queryItem