Next.js Discord

Discord Forum

Supabase data returns null even though the trigger was called on its insert event

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Avatar
Spectacled bearOP
I have a trigger/function setup with supabase and it works perfectly, however I'm calling an API point and in there I'm fetch the data that got inserted to the profile table which caused the trigger to run in the first place and for some reason it returns null firstly. I was able to solve this by a setTimeout and wait 1 second and it worked locally however on production it just wont work, how can I make sure that the data actually got inserted and I can do operations on it?

this is my code:
const handler = async (req: any, res: any) => {
  const supabase = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL as string,
    process.env.SUPABASE_SERVICE_KEY as string
  );
  const stripe = new initStripe(
    `${process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY}`,
    {
      apiVersion: "2022-11-15",
    }
  );

  const customer = await stripe.customers.create(
    {
      email: req.body.email,
    },
    {
      apiKey: `${process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY}`,
    }
  );

  if (customer) {
    try {
      setTimeout(async () => {
        const { data, error } = await supabase
          .from("profile")
          .select("*")
          .eq("email", req.body.email)
          .single();

        if (data) {
          console.log("data", data, req.body);
          const { data: userData, error } = await supabase
            .from("profile")
            .update({
              customer_id: customer.id,
            })
            .eq("id", req.body.id);
        }
        const subscriberData = {
          email: req.body.email,
          listIds: [7],
        };
      }, 1500);

      try {

        res.send({
          message: `Stripe customer created ${customer.id}`,
        });
      } catch (mailerLiteError) {
     
        console.error("Error sending data to MailerLite:", mailerLiteError);
      }
    } catch (updateError) {
      
      console.error("Error updating column:", updateError);
    }
  }
};

0 Replies