Node backend communication between react frontend and node backend | Shopify related

Viewed 51

I am developing a Shopify private app where I want to send an email on button click. So the store admin clicks on a button on my react frontend and then I call a custom api endpoint (that was defined in the node backend). I wanted to send an Email when this api endpoint is called but nothing is happening.

I mean the status code is 200 but it doesn't do anything.

Maybe someone could help me with this problem.

index.js

app.use(express.json())
applyEmailAPI(app);

email-api.js

export function applyEmailAPI(app){

app.post("/api/sendMail", async (req, res) => {
    try {
      console.log("Hello");
      res.status(201).send(response[0]);
      
    } catch (error) {
      res.status(500).send(error.message);
    }
  });
}

form.jsx

const handleSubmit = useCallback(
    (body) => {
      (async () => {
        const parsedBody = body;
        console.log("Here");
        
        const response = await fetch("/api/sendMail?shop=SHOP_URL", {
          method: "POST",
          body: parsedBody,
          headers: { "Content-Type": "application/json" }
        });
        if (response.ok) {
          console.log(response);
          console.log("Success");
        }
      })();
      return { status: "success" };
    },
   []
  );

Have a great day!

EDIT:

I managed to solve this problem. How? I will show you:

I placed this code

app.post("/api/sendMail", (req,res) => {

    verifyRequest(app, {
      billing: billingSettings,
    })
    
    res.end();
  });

above this code:

app.use(
    "/api/*",
    verifyRequest(app, {
      billing: billingSettings,
    })
  );

So it looks like this:

app.post("/api/sendMail", (req,res) => {

    verifyRequest(app, {
      billing: billingSettings,
    })
    
    res.end();
  });

app.use(
    "/api/*",
    verifyRequest(app, {
      billing: billingSettings,
    })
  );

This solved my problem.

0 Answers
Related