Front end import statements for wix members backend don't work

Question:
Front end import statements for wix members backend don’t work

Product:
Wix Editor

What are you trying to achieve:
Successfully calling myApproveByEmail and myAssignRole functions in the backend module from my front end page code.

Here is the front end page code. Note, the two import statements are currently commented out along with all other page code except for a lastName onChange handler and a buttonGo onClick handler.

import wixData from “wix-data”;
//import { myApproveByEmailFunction } from ‘backend/newSiteMember’;
//import { myAssignRoleFunction } from ‘backend/newSiteMember’;

//global variables
let wixid;
let email;

$w.onReady(function () {
//######## Register Event Handlers
//$w(“#btnGo”).onClick((event) => {
//$w(“#firstName”).focus();
//btnGo_clickHandler();
//});

});

/*
…bunch of front end code all commented out while debugging the import statements
*/

export function buttonGo_click(event) {
console.log(“Go Button clicked”);
}

export function lastName_change(event) {
console.log(“Last Name changed”);
}

On the live website, if you either fill and tab out of the lastName field OR click the buttonGo button, both event handlers fire and the console messages appear in the logs.

HOWEVER, if you simply uncomment EITHER of the two import statements, the events don’t fire. There are no errors indicated in the code and there are no errors in the live logs.

Here is the backend code in the newSiteMember.js backend file.
//Filename: backend/newSiteMember.js

import { authentication, authorization } from ‘wix-members-backend’;

export function myApproveByEmailFunction(email) {
return authentication.approveByEmail(email)
.then((sessionToken) => {
return {
sessionToken: sessionToken,
approved: true
};
})
.catch((error) => {
return {
approved: false,
reason: error
};
});
}

export function myAssignRoleFunction(wixid) {
const roleId = “dc225d03-b81b-4371-bc2c-221f1770a499”;
const memberId = wixid;
const options = {
suppressAuth: false
};

return authorization.assignRole(roleId, memberId, options)
.then(() => {
console.log(“Role assigned to member”);
})
.catch((error) => {
console.error(error);
});
}

What have you already tried:
If I run either of the functions in the backend code (by clicking the right arrow icon) in the editor, they both work just fine (the new member is approved and email sent, and a role is assigned to the new member). So, there is no problem with the newSiteMember.js code.

Additional information:
If I uncomment all of my front end code, none of it runs if either of the two backend imports are not commented out. (Nothing happens and no errors.) If they are commented out, then the front end code on the page executes.

So, this proves the problem is with the two import statements.
What is wrong with my backend import statements in my front end page code?? I am the site owner so no permission problems.

Hello! The first thing that I am noticing is that your backend file is .js

The approved backend extensions are myFileName.jsw or myFileName.web.js

Take a look at this doc for more information. And please note that .jsw is deprecated and while it will continue to work in existing sites, we do suggest writing backend code in the updated programming model moving forward.

Thanks!!! Changing it to .jsw worked fine.

May I ask why this is depreciated?

1 Like

Of course!

This change is part of a code firs development approach for the way backend code is written and handled when working on Wix or Wix Studio.

Some notable benefits include setting and see function permissions directly in your code(instead of having to do this in a UI setting).

The .web.js file format also allows for a consistent experience across environments whether you are developing locally, in the editor IDE, or using WixIDE.

Again, very important that this is not a breaking change. The code you just wrote will continue to work.

API documentation is in the process of being updated to show the web.js format in all backend code snippets which will also help aid the transition (it’s still jsw currently)

This video shows web methods in action. It’s on the Wix IDE in Wix Studio, but the code you will write would be the same in the classic editor.