【问题标题】:Filter array of orders and request PUT method to update filtered orders with javascript过滤订单数组并请求 PUT 方法以使用 javascript 更新过滤后的订单
【发布时间】: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/vls9qq

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


    【解决方案1】:

    我不确定您要完成什么。我的理解是这样的:

    1. 您正在发出 API 请求以获取订单列表。
    2. 您想过滤该列表以查找来自加拿大但没有电话号码的订单。
    3. 您想使用虚拟电话号码编辑过滤后的列表并将这些更改发送到服务器。

    我不太了解获取/下订单的 api 是如何编写的,所以我想到的可能效率不高。我会映射所有订单并在没有电话号码的情况下更改加拿大订单

    orders = orders.map(order=>{
      let {country, phone } = order.shipping_address
      if(country === "Canada" && phone === ''){
        order.shipping_address.phone = '0000000'
      }
      return order
    })
    

    而不是把filteredOrders只是把它们都放了

    $.ajax({
              method: "PUT",
              url: api_url_post,
              contentType: "application/json",
              crossDomain: true,
              dataType: "json",
              data: JSON.stringify(orders),
              success: function (data, status, jqXHR) {
                  alert("success");// write success in " "
              },
      
              error: function (jqXHR, status) {
                  // error handler
                  console.log(jqXHR);
                  alert('fail' + status.code);
              }
           });
    

    可能有一种方法可以选择性地更新符合您条件的每个订单,但我不知道您正在使用的 api

    【讨论】:

      【解决方案2】:
          $(document).ready(function () {
        var api_url = "https://api-blalbla/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);
            orders = orders.map(order=>{
              let {country, phone } = order.shipping_address
              if(country === "Canada" && phone === ''){
              order.shipping_address.phone = '0000000'
              checkCountry(order, orders)
                  console.log(orders);
                  console.log(order);
              }
              else {
                return order
              }
            
              
              
      })
          },
        });
      });
      
      function checkCountry(filteredOrder, allOrders) {
           
          var api_url_post = "https://api-blalbla/admin/api/2020-10/orders.json?status=any.json";
          
      //     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(allOrders),
                success: function (data, status, jqXHR) {
                    alert("success");// write success in " "
                },
        
                error: function (jqXHR, status) {
                    // error handler
                    console.log(jqXHR);
                    alert('fail' + status.code);
                }
             });
                  
      }
      

      到目前为止,这是我的代码,尝试将所有订单放入数据中,就像你所说的在 put request 时仍然出现同样的错误

      https://prnt.sc/vm3edm

      另外,我认为你可以通过 id 获得特定订单

      /admin/api/2020-10/orders/{order_id}.json
      

      所以 mby 我得到过滤的订单 ID 并在 url 上输入参数并使用电话“00000”更新该订单。这应该有效吗?

      【讨论】:

        【解决方案3】:

        是的,它是这样工作的

        Also, I figured that u can get specific order by id
        
        /admin/api/2020-10/orders/{order_id}.json
        So mby i get the filtered order id and put in parameter on url and update that order with phone "00000". Should this work?
        

        非常感谢,这段代码帮助了我,我很感激! :)

           orders = orders.map(order=>{
          let {country, phone } = order.shipping_address
          if(country === "Canada" && phone === ''){
            order.shipping_address.phone = '0000000'
          }
          return order
        })
        

        【讨论】:

          【解决方案4】:

          更新:是否只能从数组中获取第一个订单?

          // mby like except .map to [0]
          orders = orders[0](order=>{
            let {country, phone } = order.shipping_address
            if(country === "Canada" && phone === ''){
              order.shipping_address.phone = '0000000'
            }
            return order
          })
          

          我需要这样,所以如果商店有 20k 个订单,它就不会检查所有订单。只有第一个(最新的)

          【讨论】:

            猜你喜欢
            • 2018-05-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-08-16
            • 1970-01-01
            相关资源
            最近更新 更多