Can I import/use wix-storage inside my event.js file?

Hello, I’m trying to pull data saved from a session from a wix’s page into one of my functions in the events.js file (I have wixStores_onNewOrder event in my event.js file).

However, when I include my import of wix-storage and try to pull data, it somehow fails. By fail, I mean no data was pulled at all and my function/event is not working anymore.

// Place this code in the events.js file
// of your site's Backend section. 
// Add the file if it doesn't exist.

// Place this code in the events.js file
// of your site's Backend section.

import wixData from 'wix-data';
import {session} from 'wix-storage';

export async function wixStores_onNewOrder(event) {
  const newOrderId = event.orderId;
  const dateCreate = event.dateCreated;

  // Get the new order's line items from the Stores/Orders collection
  const orderLineItems = await wixData.get("Stores/Orders", newOrderId)
    .then((results) => {

        let srcReferralVal = session.getItem("srcreferral"); // "value"
        
        let newOrder = {
            orderId        : results.number,
            dateCreated    : dateCreate,
            buyerName      : results.buyerInfo.firstName.concat(" ", results.buyerInfo.lastName),
            buyerFName     : results.buyerInfo.firstName,
            buyerSName     : results.buyerInfo.lastName,
            buyerEmail     : results.buyerInfo.email,
            buyerPhone     : results.buyerInfo.phone,
            orderItems     : results.lineItems,
            shippingInfo   : results.shippingInfo,
            shipAddress    : results.shippingInfo.shipmentDetails.address.formatted,
            shipCountry    : results.shippingInfo.shipmentDetails.address.country,
            shipProvince   : results.shippingInfo.shipmentDetails.address.subdivision,
            shipCity       : results.shippingInfo.shipmentDetails.address.city,
            shipZip        : results.shippingInfo.shipmentDetails.address.postalCode,
            shipBrgy       : results.shippingInfo.shipmentDetails.address.addressLine,
            billingInfo    : results.billingInfo,
            billingAddress : results.billingInfo.address.formatted,
            paymentMethod  : results.billingInfo.paymentMethod,
            totals         : results.totals,
            shipAmt        : results.totals.shipping,
            discountAmt    : results.totals.discount,
            netAmt         : results.totals.total,
            lastUpdated    : results._updatedDate,
            paymentStatus  : results.paymentStatus,
            dbInserted     : '0',
            sharer         : srcReferralVal

        };
        console.log(results.items);
        wixData.insert("TestOrders", newOrder);
      return results.lineItems
    })
    .catch((error) => {
      // Order not found in the Stores/Orders collection
      console.error(error);
    });
}

The session variable ‘srcreferral’ is saved from another page (Products Page).

Please advice. Thank you very much!

The events.js file is in the backend and the wix-storage API is only available from Page (frontend) code. The code in events.js does not know anything about the site pages.

Hi @yisrael-wix – thanks for confirming that I cannot pull data saved in session from Page (frontend) code into events.js. However, may I know if you have any suggestion in mind to copy/pull data from frontend page code into backend such as the events.js file?

@ppolendey Events run independent of pages and an event should have all of the data that you need. However, you might be able to use a database collection to act as persistent storage. You can use the _id of the collection’s item to associate the necessary data with the relevant event.

@yisrael-wix – the data i would need to store is from the query section of the URL from the frontend pages (I get it via wix-location). Is that something that I can incorporate with events as well? Thank you.