currentMember not returning anything

I’m trying to run the script below using the ecom-custom-discount-trigger SPI to apply a discount on all products when the current logged in user is a paid member (has a subscription plan) on my website.

I know it is a paid member when one of their member roles (currentMember.getRoles()) has the same id as the member role I created on my dashboard specifically for this purpose.

But it actually does not return anything from the current member, as if there was no logged in user. Obviously I tried it on production and logged in with a test account, but the log says the function is no returning any logged user.

I’m new to javascript, so any help would be very much appreciated. I actually don’t what else to try.

Thanks!

import * as ecomDiscountsTrigger from 'interfaces-ecommerce-v1-custom-trigger';
import { currentMember } from 'wix-members-backend';

export const listTriggers = async (context) => {

    const result = {
        customTriggers: [{
            _id: "1",
            name: "Paid Member"
        }]
    }
    console.log('result', result)
    return result
};

export const getEligibleTriggers = async (options, context) => {
    const result = getRoleIds()
        .then((roles) => {
            if (roles.includes("ad3ec1fe-6529-485a-ac53-68ec63ce687b")) {
                return {
                    eligibleTriggers: [{
                        customTriggerId: "1",
                        identifier: options.triggers[0].identifier
                    }]
                }
            } else {
                return {
                    eligibleTriggers: []
                }
            }
        })
        .catch(() => {
            return {
                eligibleTriggers: []
            }
        })

    return result

};

export async function getRoleIds() {
    return currentMember.getRoles()
        .then((roles) => {
            return roles.map(role => role._id)
        })
        .catch((error) => {
            console.error(error);
            return [""]
        });
}

At first glance I don’t see anything incorrect with this code.

Can you share how this code is being called from the frontend? Is this code in a Velo Web Module?

Open your MASTER-PAGE and add the following into it…

MASTER-PAGE-CODE:

import wixMembers from 'wix-members';


$w.onReady(async()=>{console.log("Master-Page running...");   

    //-----On-Login-Section-----------------------------------
    wixMembers.authentication.onLogin(async(member)=> { 
       console.log("MEMBER: ", member);
       let memberData = await member.getMember();
       console.log('MEMBER-Data: ', memberData);      
       const memberRoles = await member.getRoles();
       console.log('MEMBER-ROLES: ', memberRoles);  
       const memberID = memberData._id;
       console.log('MEMBER-ID: ', memberID);
       const memberProfile = memberData.profile;
       console.log('MEMBER-PROFILE: ', memberProfile);
       const memberDetails = memberData.contactDetails;
       console.log("MEMBER-DETAILS: ", memberDetails);     
       memberData.memberID = memberID;
       memberData.memberRoles = memberRoles;
       memberData.memberBadges = memberBadges;
    //----------------------------------------------------
    console.log('FINAL-MEMBER-DATA: ', memberData);

   });


So normaly you even do not need to set it up on backend, because you can get almost all needed MEMBER-INFO also directly on FRONTEND after the user has logged-in.

But if you sill want to use your way…

$w.onReady(async()=>{console.log("Master-Page running...");   

    //-----On-Login-Section-----------------------------------
    wixMembers.authentication.onLogin(async(member)=> { 
         ............your code here to call the backend-function...................
    });
});