'SyntaxError: Unexpected end of JSON input on JSON.stringify

im getting error

Looking for new purchases
new order appears
undefined:1


SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Response.json (file:///C:/Users/Admin/Documents/Allegro/server/node_modules/node-fetch/src/body.js:149:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async default.request (file:///C:/Users/Admin/Documents/Allegro/server/modules/allegroAPI.js:90:16)
    at async default.sendPurchasedProducts (file:///C:/Users/Admin/Documents/Allegro/server/modules/allegroAPI.js:166:7)
    at async Timeout._onTimeout (file:///C:/Users/Admin/Documents/Allegro/server/modules/allegroAPI.js:155:9)

and some code

  async request(path, method, payload) {
    if (this.props?.auth_token.expire <= Date.now()) return this.init();

    const response = await fetch(this.props.api_url + path, {
      method,
      body: JSON.stringify(payload),
      headers: {
        "Content-Type": "application/vnd.allegro.public.v1+json",
        Accept: "application/vnd.allegro.public.v1+json",
        Authorization: `Bearer ${this.props.auth_token.token}`,
      },
    });
    let data = await response.json();         //line 90
    return data;
  }
  async loopChestOrders() {
    setInterval(async () => {
      let data = await this.request(
        "order/checkout-forms?status=READY_FOR_PROCESSING&fulfillment.status=NEW",
        "GET"
      );
      if (data.count > 0) {
        console.log("new order appears");
        await this.sendPurchasedProducts(data);   //line 155
      } else {
        console.log("Looking for new purchases");
      }
    }, 1000);
  }

  async sendPurchasedProducts(data) {
    let orders = data.checkoutForms;
    for (let order of orders) {
      const payloadProcessing = { status: "PROCESSING" };
      await this.request(`order/checkout-forms/${order.id}/fulfillment`, "PUT", payloadProcessing); //line 166

      const codes = [
        {
          name: "TestCode",
          codes: ["Code", "Code"],
        }
      ];

      const payloadSent = { status: "PICKED_UP" }

      sendMail(order.buyer.login, order.buyer.email, codes);
      await this.request(
        `order/checkout-forms/${order.id}/fulfillment`,
        "PUT",
        payloadSent
      );
    }
  }

and line 166 executes correctly before error becouse api received request data is also correct when passing to sendPurchasedProducts it happens when {status: "processing"} or {status: "PICKED_UP"} is in code but when i remove both code do not throw error but i cant remove it becouse api cant process it without that JSON



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source