I am having trouble returning an object to front end from a function on my backend. (The password function in the code is just for testing purposes).
What I am doing is receiving some data on another platform (stripe) then getting it over to wix and registering the customer. Then, I want to return the “registrationResult” object to the front end so I can then log the user in with code and redirect him/her to a specific member’s page with path /myplan.
The PROBLEM I am having is that the “registrationResult” on the front end side comes out as UNDEFINED.
I do not understand because whenever I run the code from the backend console (image attached) I do get a return value but whenever I try it live it comes out as undefined.
Code in BACKEND:
export async function updateGuarCustomersInDB(checkoutSession) {
var returnValue
const session = await key.checkout.sessions.retrieve(checkoutSession);
const customer = await key.customers.retrieve(session.customer);
function generatePassword() {
var length = 8,
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}
let password = generatePassword();
await authentication.register(customer.email, password, {
"contactInfo": {
"firstName": first,
"lastName": last
}
})
.then((registrationResult) => {
returnValue = registrationResult;
console.log(returnValue);
assignRole("ACTIVE", registrationResult.member._id)
.then(() => {
})
.catch((err) => {
//console.log(err);
});
})
.catch((error) => {
//console.error(error);
})
return returnValue;
}
Code in FRONT END
if(wixLocation.query.checkoutSession != null){
$w('#section22').collapse();
$w('#section21').collapse();
$w('#section24').expand();
updateGuarCustomersInDB(wixLocation.query.checkoutSession)
.then((registrationResult) => {
authentication.applySessionToken(registrationResult.member.sessionToken)
.then(() => {
wixLocation.to('/myplan');
});
});
} else {
$w('#section24').collapse();
}
Image of code being ran on the backend console:
You can see the object that is being returned whenever I run the function. But if I try it live the image below happens.
Image of the error i get when i try that code live:
I have been struggling with this. I do not know how to fix it. Please help me find a solution or a work around.
I also tried to log the user in on the backend but for some reason it would not let me access the /myplan page. It redirected me to the default login page wix offers but since I have a custom login then that would not work.