Search code examples
node.jsstrapi

Strapi change field value in afterCreate hook


I am calling Stripe api every time the user creates a new product in Strapi, and that works perfectly.

What I would like to do is I would like to assign the Stripe ID from the response to a StripeId field inside Strapi after the response has arrived.

I tried this but it doesn't seem to be working:

module.exports = {
  lifecycles: {
    async afterCreate(result) {
      const product = await stripe.products.create({
        name: result.title,
      });
      result.StripeId = product.id;
    },
  },
};

Any idea how to do this?

Thanks!


Solution

  • The data needs to be altered before writing to DB, so this is the solution in case anyone needs to implement something similar to this:

    module.exports = {
      lifecycles: {
        async beforeCreate(data) {
          const product = await stripe.products.create({
            name: data.title,
          });
          data.StripeId = product.id;
        },
      },
    };
    

    Cheers!