How to get user details when we triggered onPlanPurchased() in backend

https://www.wix.com/velo/reference/wix-paid-plans-backend/events/onplanpurchased

event.orders…

Hey @subasskutti :raised_hand_with_fingers_splayed:

You can retrieve it from the triggered event . Then use the getUser( ) to get his full details.

// Backend: events.js
import { getUser } from 'wix-users-backend';

export async function wixPaidPlans_onPlanPurchased(event) {
    const user_id = event.order.memberId;
    const plan_owner = await getUser(user_id);
}

Hope this helps~!
Ahmad

i got a error by using (await)

@subasskutti
Inspect first the result what you get from → EVENT.
Check if → event.order.memberId ← is given in the result.
What do you get as output for → userID ?
Place perhaps a second → AWAIT, like in the example and test it again…

export async function wixPaidPlans_onPlanPurchased(event) {console.log(event);
    const userID = await event.order.memberId; console.log(userID);
    const planOwner = await getUser(userID); console.log(planOwner);
}

@subasskutti that’s definitely because you didn’t change the function into an async function. Notice the orange keyword.

export async function wixPaidPlans_onPlanPurchased(event) {

}

@russian-dima async/await is used with functions that returns a promise, accessing a property does not require using await.

@ahmadnasriya You might be correct → but for those, who do not know, if you have a promise in the situation or not, just to be sure, that it will work, i just place an additional AWAIT :grin: → it does not crash the code. Unneccessary → yes perhaps, but secure :laughing:

@russian-dima this is a bad practice, to use await you must change the function to an async one, which makes the function returns a promise.

You should always know what type of response the function returns (void/promise), and write your code according to that.

@ahmadnasriya How do i know that ? Just always by taking a look into the API-DOCs?

thanks bro
this my mistake ididn’t type async

@russian-dima you can know the response type by looking at the function itself.

You’re welcome :wink: Did this solve your problem?

@ahmadnasriya no bro i got one more error
Cannot find module ‘wix-users-backend’ in ‘public/pages/gdsrt.js’

@subasskutti as the documentations state, the events must reside on the backend in a file called “events.js”.

@subasskutti Please read the APIs docs carefully before using them, it’ll help you avoid getting stuck with simple things.

@ahmadnasriya ok bro i will read