Is it possible to use/call functions from events.js?

Hi! Is it possible to use functions from events.js and import them to other backend files?

Or is it possible to integrate a new function within events.js?

For instance I want to use this function and import into other backend code:

export function wixEcom_onCheckoutCreated(event) {
  const checkoutSubtotal = event.entity.subtotal.amount;
  const checkoutId = event.entity._id;
  const eventId = event.metadata.id;
  console.log("Checkout created", event);
}

If yes how may I able to use events functions? Thanks.

Events are ‘special’ functions and must be used within an events.js file to work. You’re not able to call them from other functions, since they’re only triggered based on other things (like checkout created in your above example).

What you can do is call functions from other backend code within the event.

Are you able to share more of what you’re hoping to achieve?

1 Like

I am trying to use getCheckout( ) and since onCheckoutCreated( ) triggers when a new checkout is created and can return checkoutId. I will use this checkoutId as parameter to get checkout details basically.

I want to pass in the checkoutId from the event function to the getCheckout()
in reference to this documentation for it, however it still wouldn’t work:

backend/getCheckout.jsw

import { checkout } from "wix-ecom-backend";

export async function myGetCheckoutFunction(checkoutId) {
  try {
    const retrievedCheckout = await checkout.getCheckout(checkoutId);
    console.log("Success! Retrieved checkout:", retrievedCheckout);
    return retrievedCheckout;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
}
import { myGetCheckoutFunction } from "backend/getCheckout.jsw";

export function wixEcom_onCheckoutCreated(event) {
    const checkoutId = event.entity._id;
    const eventId = event.metadata.id;

    console.log("Checkout created:", event);
    console.log("checkoutId:", checkoutId);
    console.log("eventId:", eventId);

//Calling other functions from backend wont work.
    myGetCheckoutFunction(checkoutId)
        .then((checkout) => {
            console.log("Success! Retrieved checkout:", checkout);
            return checkout;
        })
        .catch((error) => {
            console.error(error);
            // Handle the error
        });

}

Hi, @user4439 !!

I’m not sure if I fully understand what you’re trying to do, but you seem to be trying to return a value within a function triggered inside event.js. The key question might be: Where are you expecting this return value to go? :upside_down_face:

Since the obtained value does not automatically get sent anywhere, you might need to store it in a collection or use Wix Realtime to send it to the frontend. Without such an approach, the process in event.js will remain confined to the backend, and it may seem like nothing is happening. :upside_down_face:

If you want to check immediately whether myGetCheckoutFunction is being triggered, you can use Wix’s backend logging feature to observe its execution. Try opening the logs and triggering myGetCheckoutFunction—you should see the console.log output appear.

Also, this may not be directly related to the current issue, but if you want to return the execution result of myGetCheckoutFunction to the frontend, I think you’ll need a return statement not only inside the function but also before calling myGetCheckoutFunction.

Additionally, .jsw files are primarily meant for calling backend functions from the frontend, so using .jsw isn’t necessarily required. If myGetCheckoutFunction is only being used within the backend, you could place it in a regular .js file instead. :raised_hands: :smiling_face_with_sunglasses: