【发布时间】:2020-11-18 22:39:43
【问题描述】:
我很难弄清楚如何过滤和更新已过滤订单的 API:
在 Shopify 订单面板中,我进行了 API 调用并获得了所有订单的列表。我实现只使用 put 方法更新 1 个数组,但它不是动态的。所以基本上我想做的是:
顺便说一句,这都是javascript
获取所有订单的 api 调用,然后过滤具有加拿大国家和空白电话号码的订单,然后在那些带有加拿大和空白号码的订单上,我尝试使用 PUT 方法更新它们以将号码更改为“虚拟号码”但是不能仅将其应用于具有加拿大和空白编号的订单。我确实收到了所有订单,并且得到了 6 个订单的对象数组。这是我目前的代码。
$(document).ready(function () {
// this is valid url and returns all orders
var api_url = "https://($api-domain-bla-bla)/admin/api/2020-10/orders.json?status=any.json";
$.ajax({
url: api_url,
contentType: "application/json",
dataType: "json",
success: function (result) {
console.log(result);
// get all orders phone country
let orders = result.orders;
console.log(orders);
for(let i = 0; i < orders.length;i++) {
let phone = orders[i].shipping_address.phone;
let country = orders[i].shipping_address.country;
// here i am trying to filter them right away but don't know how to make array of filter objects
// the if statement works
if ( country === "Canada" && phone === '') {
// over here i am trying to do something to those objects which if statement is true.
let filteredOrder = orders[i].concat
console.log(orderId);
// a function with parameter of the filtere objects variable, to the api PUT method to update
// the filter orders/objects
checkCountry(filteredOrder);
}
// console.log(phone);
// console.log(country);
// checkPhone(phone, country);
}
},
});
});
这是我从 console.log(result) 得到的
https://prnt.sc/vlsh55(所有订单的api)
function checkCountry(order) {
// here i want to update the orders with canada and blank phone number, to put "00000" on the phone
//number i had code here with some if statements but i figured it wouldn't work so i am trying new way
//to get it to work
var api_url_post = "https://($api-domain-bla-bla)/admin/api/2020-10/orders.json?status=any.json";
// this phone variable the structure is the same with the one i get from the call above, don't know
// why it doesn't accept it.
var phone = {
"orders":
{
// "id": 1843613401136,
// "phone": "+0000000000",
"shipping_address": {
"phone": "0000000000"
}
},
}
$.ajax({
method: "PUT",
url: api_url_post,
contentType: "application/json",
crossDomain: true,
dataType: "json",
data: JSON.stringify(phone),
success: function (data, status, jqXHR) {
alert("success");// write success in " "
},
error: function (jqXHR, status) {
// error handler
console.log(jqXHR);
alert('fail' + status.code);
}
});
}
对不起,如果我说不清楚,有什么帮助吗?
【问题讨论】:
标签: javascript arrays rest object shopify-api