Community X.

Connect with other creators, share ideas, give feedback and get the latest product updates.

Jan 14

Error when calling a function from the backend

Hi, I am trying to make an eWallet system for my website and I want to use the backend to ensure the safety of my website. I have a very simple function (currently without much validation) that allows the user to give "credits" to another user. The problem I have is when I call the function (as I have imported it from the backend to one of the pages) I get the following error:

This is the code that I am trying to call from the backend (I have changed the name of the collection):


// Filename: backend/EWallet.jsw (web modules need to have a .jsw extension)
import wixUsers from 'wix-users'
import wixData from 'wix-data'

export async function PayWithEWallet(otherUserEmail, amount)
{
 return new Promise(async(resolve) => {

 if(wixUsers.currentUser.loggedIn === true)
        {
 await wixUsers.currentUser.getEmail()
            .then(async(email)=>{
 await wixData.query("NameOfTheCollection")
                .eq("email", email)
                .find()
                .then(async(result) =>{
 if(result.length === 1)
                    {
 // TO DO - Convert amount -> tockens 
 var tockens = amount;

 var UserBalance = result.items[0].amount;
 if(tockens <= UserBalance && tockens > 0)
                        {
 await SubtractAmountFromWallet(email, tockens);

 await AddAmountToWallet(otherUserEmail, tockens);
                        }
                    }
                })
                .catch(err =>{
                    console.log(err.message);
                });
            })
            .catch(err =>{
                console.log(err.message);
            });
        }

        resolve();
    });
}

async function SubtractAmountFromWallet(email, amount)
{
 return new Promise(resolve => {
        wixData.query("NameOfTheCollection")
        .eq("email", email)
        .find()
        .then(result =>{
 if(result.length === 1)
            {
 var UserWallet = result.items[0];
                UserWallet.amount-=Math.abs(amount);
                wixData.update("NameOfTheCollection", UserWallet);
            }
        });
        resolve();
    });
}

async function AddAmountToWallet(email, amount)
{
 return new Promise(resolve => {
        wixData.query("NameOfTheCollection")
        .eq("email", email)
        .find()
        .then(result =>{
 if(result.length === 1)
            {
 var UserWallet = result.items[0];
                UserWallet.amount+=Math.abs(amount);
                wixData.update("NameOfTheCollection", UserWallet);
            }
        });
        resolve();
    });
}

The weird thing is that even if I call the function "PayWithEWallet" with all of its body commented out (apart from the promise) I still get the same error and if I do move the entire code from the backend to the frontend, it is working properly.

I am importing the function:



import {PayWithEWallet} from "backend/EWallet.jsw"

And I am using the function by creating a new function that I attach to a button:


export function click_TokenPayment()
{
    PayWithEWallet(otherUser.email, 50);    
}

I have verified that the data I am sending is correct as the code works properly when it is in the frontend.


I would really appreciate any ideas about how to solve this because I would really prefer not to be exposing the code to all of these functions in the frontend!

1 answer0 replies
2
Jan 15Edited: Jan 17

Ok I found the error. I should not be using


import wixUsers from 'wix-users'

Because 'wix-users' works only on the front end. Instead, I should be using 'wix-users-backend' as this is the package that works on the backend.

Editor X

Design your boldest creations.