簡體   English   中英

使用 Javascript 和 Zapier 處理 null 值

[英]Handling null values with Javascript and Zapier

我正在為 Zapier 編寫一個腳本,我想在其中傳遞訂單信息。 我最多支持三個訂單項目。 問題是如果只有一個訂單項目,仍然為另外兩個項目傳遞空白值,從而創建空白行。 這是我到目前為止所擁有的:

  url: 'https://edapi.campaigner.com/v1/Orders',
  method: 'POST',

  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'X-API-KEY': bundle.authData.ApiKey
    },
    params: {
    'ApiKey': bundle.authData.ApiKey
    },
    body: {
  'EmailAddress': bundle.inputData.EmailAddress,
  'OrderNumber': bundle.inputData.OrderNumber,
  'PurchaseDate': bundle.inputData.PurchaseDate,
  'TotalAmount': bundle.inputData.TotalAmount,
  'Items':[
      {"ProductName": bundle.inputData.ProductName,
      "SKU": bundle.inputData.SKU2,
      "Quantity": bundle.inputData.Quantity,
      "UnitPrice": bundle.inputData.UnitPrice,
      "Status": bundle.inputData.Status},
      
      {"ProductName": bundle.inputData.ProductName2,
      "SKU": bundle.inputData.SKU2,
      "Quantity": bundle.inputData.Quantity2,
      "UnitPrice": bundle.inputData.UnitPrice2,
      "Status": bundle.inputData.Status2},
      
       {"ProductName": bundle.inputData.ProductName3,
      "SKU": bundle.inputData.SKU3,
      "Quantity": bundle.inputData.Quantity3,
      "UnitPrice": bundle.inputData.UnitPrice3,
      "Status": bundle.inputData.Status2},
  ]
    
    }

};


return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = response.json;
    // You can do any parsing you need for results here before returning them
    return results;
  });


我在想我需要一個可能的 if 語句或什么?

我能夠在這方面獲得幫助。

const options = {
    url: 'https://edapi.campaigner.com/v1/Orders',  
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-API-KEY': bundle.authData.ApiKey
    },
    params: {
        'ApiKey': bundle.authData.ApiKey
    },
    body: {
        'EmailAddress': bundle.inputData.EmailAddress,  
        'OrderNumber': bundle.inputData.OrderNumber,
        'PurchaseDate': bundle.inputData.PurchaseDate,
        'TotalAmount': bundle.inputData.TotalAmount,
        'Items':[],
    }
};
// Add Items to array if they exist
if (bundle.inputData.ProductName) {
    options.body.Items.push({
        "ProductName": bundle.inputData.ProductName,
        "SKU": bundle.inputData.SKU,
        "Quantity": bundle.inputData.Quantity,
        "UnitPrice": bundle.inputData.UnitPrice,
        "Status": bundle.inputData.Status});
}
if (bundle.inputData.ProductName2) {
    options.body.Items.push({
        "ProductName": bundle.inputData.ProductName2,
        "SKU": bundle.inputData.SKU2,
        "Quantity": bundle.inputData.Quantity2,
        "UnitPrice": bundle.inputData.UnitPrice2,
        "Status": bundle.inputData.Status2});
}
if (bundle.inputData.ProductName3) {
    options.body.Items.push({
        "ProductName": bundle.inputData.ProductName3,
        "SKU": bundle.inputData.SKU3,
        "Quantity": bundle.inputData.Quantity3,
        "UnitPrice": bundle.inputData.UnitPrice3,
        "Status": bundle.inputData.Status3});
}
return z.request(options)
    .then((response) => {
        response.throwForStatus();
        const results = response.json;
        // You can do any parsing you need for results here before returning them
        return results;
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM