What am I doing wrong. Have simplified this for the example:
Front End:
import {deleteMember} from 'backend/backendServices'; export function btnRemoveMember_click(event) { var userId = '' deleteMember(userId) .then(function(result) { $w("#txtTest").text = "result: " + result; })
}
Backend Example A: (DOES NOT RETURN ANYTHING)
export function deleteMember(userId) { wixUsersBackend.deleteUser(userId) .then( () => { return "User has been deleted" }) .catch( (err) => { return JSON.stringify("User has failed delete attempt!: " + err); }); }
Backend Example B: (RETURNS/WORKS JUST FINE)
export function deleteMember(userId) { return "hello" }
It works fine (I get the return string) when i run a command in the backend that does not have the promise resolution (.then/.catch structure) - Example B. But when I include it as shown above in Example A, the front end does not get the return values. The back-end function executes its task just fine (or not fine in the example above where the userID is null), but the front end does not know what the result is.
I have a feeling this is something really simple that I am overlooking. But rather than sit and stew for any longer, I feel I need to ask for help.
thanks.
Never mind. I found the answer: https://support.wix.com/en/article/corvid-web-modules-calling-server-side-code-from-the-front-end
However, the example provided in the link does not consider the error return from the original fetch function. When I try to incorporate the solution using a full .then/.catch approach, it did not work.
It's not pretty and I don't really like it, but this is how I got it to work:
BACKEND FUNCTION:
export async function deleteMember(userId) {
var success = false;
var errormsg = '';
await wixUsersBackend.deleteUser(userId)
.then( () => {
success = true;
})
.catch( (err) => {
errormsg = "User has failed delete attempt!: " + err;
});
if (success) { return "success!"; } else { throw new Error(errormsg); }
}
I am COMPLETELY open to another - better - way.