"Object reference not set to an instance of an object" 尝试使用 ShipStation 的 API 更新产品时出错

"Object reference not set to an instance of an object" error when trying to update a Product with ShipStation's API

我在 Shipstation 中更新产品时遇到问题。

我一直在使用这 2 个链接来查看我应该如何格式化响应:

https://www.shipstation.com/docs/api/products/update/ https://www.any-api.com/shipstation_com/shipstation_com/docs/Products/_products_productId_/PUT

我相信我没看错,但我总是收到一条 500 错误消息,提示“对象引用未设置到对象的实例”。

我一直在使用 GET 请求来获取产品的属性。 然后我更新需要更改的属性,并将其存储在 data(这是一个对象数组)中。然后我使用PUT请求发送数据。

这是相关代码:

function updateProducts(authString, data) {

  var baseProductUrl = `https://ssapi.shipstation.com/products/`;

  for(var d = 0; d < data.products.length; d++) { //for each product I'd like to update...

    var raw = data.products[d];
    raw = JSON.stringify(raw);

    var requestOptions = { 

      method: 'PUT',
      headers: {
        "Authorization": `Basic ${authString}`,
        "Content-Type": `application/json`,       
      },

      body: raw,
      redirect: 'follow'
    };

    var productUrl = `${baseProductUrl}${data.products[d].productId}`;
    UrlFetchApp.fetch(productUrl, requestOptions);
  }
}

这就是 raw(我发送到 ShipStation 的数据)的样子: 空值就是我从 GET 请求中收到的值。我希望这些属性保持为空。

{"aliases":null,
"productId":123456789, //placeholder
"sku":"sku", //placeholder
"name":"UV Bulb - 1GPM - 10\"",
"price":19.99,
"defaultCost":null,
"length":2,
"width":2,
"height":13,
"weightOz":7,
"internalNotes":null,
"fulfillmentSku":null,
"active":true,
"productCategory":null,
"productType":null,
"warehouseLocation":null,
"defaultCarrierCode":null,
"defaultServiceCode":null,
"defaultPackageCode":null,
"defaultIntlCarrierCode":null,
"defaultIntlServiceCode":null,
"defaultIntlPackageCode":null,
"defaultConfirmation":null,
"defaultIntlConfirmation":null,
"customsDescription":"UV Bulb - 1GPM - 10\"", //attribute I'd like to update
"customsValue":9.99, //attribute I'd like to update
"customsTariffNo":null,
"customsCountryCode":"US",
"noCustoms":null,
"tags":null}

那么有没有人有任何提示,或者有没有人以前使用过 ShipStation 的 API 并完成了 PUT 请求? 我错过了什么?

在你的脚本中,做如下修改怎么样?

发件人:

var requestOptions = { 

  method: 'PUT',
  headers: {
    "Authorization": `Basic ${authString}`,
    "Content-Type": `application/json`,       
  },

  body: raw,
  redirect: 'follow'
};

收件人:

var requestOptions = { 
  method: 'PUT',
  headers: {
    "Authorization": `Basic ${authString}`,
  },
  payload: raw,
  contentType: "application/json",
};

参考: