I have been trying to filter the results of an API call based on my "note" value. I've been building it on Zapier and the call works but I cannot seem to find a way to make a filter function do its job (so if I replace line 19-23 with return results
; then it gives me all orders from the api call). I've poured over every stack document I could find but they all end with the error result.filter not found
, or a bargle
error (generic error in Zapier).
const options = {
url: `.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;
});
I have been trying to filter the results of an API call based on my "note" value. I've been building it on Zapier and the call works but I cannot seem to find a way to make a filter function do its job (so if I replace line 19-23 with return results
; then it gives me all orders from the api call). I've poured over every stack document I could find but they all end with the error result.filter not found
, or a bargle
error (generic error in Zapier).
const options = {
url: `https://mystorename.myshopify./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;
});
And this is an example of my current output with return results;
and no filter:
{
"orders": [
{
"note": "555-5555",
"subtotal_price": "1.00"
},
{
"note": "555-6666",
"subtotal_price": "2.00"
}
]
}
Again the goal is to filter by the value in the "note" key. So if my filter input is 555-5555 then it should return all information for that item only. I did try to use an if statement for return, stringify instead of parse, covert to array...all with needed code, but regardless of the format I find filter does not work or nothing is returned. Going to keep working on it, so if I happen to find the answer I will post that, but at this point I feel stuck.
Share Improve this question edited Oct 1, 2019 at 18:26 littlecoder asked Oct 1, 2019 at 17:38 littlecoderlittlecoder 3562 silver badges17 bronze badges 2-
5
try
results.orders.filter
– Taki Commented Oct 1, 2019 at 17:40 -
No joy, it returns an undefined. Doubled checked my output and it is as I posted but to get the each item I had to put in
return results.orders[0];
orreturn results.orders[1];
. – littlecoder Commented Oct 1, 2019 at 17:55
3 Answers
Reset to default 3You are trying to use the method filter
in a object but filter is only available in an array so you should try to call filter
in the orders array.
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)
Updated to contain a real http call:
const url = 'http://www.mocky.io/v2/5d9466142f000058008ff6b7'
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
}
const response = await fetch(url, options)
const results = await response.json()
const queryItem = "555-5555"
const filteredOrders = results.orders.filter(item => item.note === queryItem)
console.log(filteredOrders)
You are trying to filter on results
, but according to your output, you should be filtering on results.orders
.
const filteredOrders = results.orders.filter(item => item.note === queryItem);
Are you getting all the orders back (all the orders with the specified filter value)?
I realized I wasn't getting back all orders and this did the trick:
`https://mystorename.myshopify./admin/orders.json?status=any`
Alternatively, you can query the orders with that specific note:
`https://mystorename.myshopify./admin/orders.json?status=any¬e=` + queryItem
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399589a4572319.html
评论列表(0条)