One netlify function does not work - Unexpected end of JSON input - Function not found

Hello,

I have been working on a react project with some netlify functions which works except one function that is not found despite beeing deployed to netlify, it does not work locally and when deployed. If you require the site URL let me know.

this is the function that calls the netlify function:

 let result = await fetch('/api/add-to-cart', 
    {
      method: 'post',
      body: JSON.stringify({ cartId: localCartData.cartId, variantId: id, quantity: quantity })
    });

A note is that the function above works on postman when using the direct url to shopify storefront API using a graphql body.

this is the function that is deployed on netlify, a note is that the rest of the functions works except this one. I have been trying to solve this matter for some time now but i have not found any solutions so far.

 const data = await sendShopifyStorefrontRequest({
    query: `
      mutation AddToCart($cartId: ID!, $variantId: ID!) {
        cartLinesAdd(cartId: $cartId, lines: [{ quantity: $quantity, merchandiseId: $variantId}]) {
          cart {
            lines(first: 100) {
              edges {
                node {
                  id
                  quantity
                  merchandise {
                    ... on ProductVariant {
                      product {
                        title
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    `,
    variables: { cartId, variantId, quantity },
  });

  return {
    statusCode: 200,
    body: JSON.stringify(data),
    headers: {
      "Access-Control-Allow-Origin": '*', // Allow from anywhere 
      "Access-Control-Allow-Methods": "*",
      "Access-Control-Allow-Headers": "*",
    }
  };
}

netlify toml:


[functions]
node_bundler = "esbuild"

[[redirects]]
from = "/api/*"
to = "/.netlify/functions/:splat"
status = 200

[build]
functions = "netlify/functions"
SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Runtime.handler (/var/task/netlify/functions/add-to-cart.js:5984:48)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)

That and the steps to reproduce this issue would be helpful.

Hello, sorry for the late reply.

I have resolved this matter, i used the api calls directly inside the React component instead of making a netlify function, just for this particular function.

1 Like