This used to work fine before - perhaps some permission setting were tightened up on Wix side?
The front-end code runs from a Dashboard page.
How can I change the back-end to suppress the restricted permissions? I’m not sure how to use suppressAuth in this case.
Advice appreciated
Frontend
getAdvisoryLevel (countryIso)
Backend
import {fetch} from 'wix-fetch';
export function getAdvisoryLevel(country) {
const url = 'https://www.travel-advisory.info/api?countrycode=';
let fullUrl = url + country;
return fetch(fullUrl, {method: 'get'})
.then(response => {
//if (response.ok) {
return response.json();
// }
});
}
Where in the frontend do you call the backend function? How do you call it? When?
You will need to provide more information in order to get assistance.
You aren’t invoking any API that would need suppressAuth.
Is the backend function actually being called?
What are the permissions on your web module (.jsw file)?
When was it working? And when did it stop?
What is the fetch returning? Is there an error message or code?
Thank you for the response. I’ll try my best to add some more detail.
It stopped working in Live a few months ago - the only error visible in browser console is HTTP/3 400 Bad Request during Live. The code still runs as expected in Preview mode.
I thought permission might be the problem as in Live mode there is never a response and it seems the backend call is never made. The console log line shows the json response - which works correctly in preview mode.
The code is run from a Dashboard page (on button click) that passes an iso2 country code to a backend function which then provides a json response. The button click event actually queries a (read-only) dataset and then steps through each result to pass the ios2 country code to the backend function. It then compares the returned result with the current value and if required updates it. Super simple.
The backend module permissions is set to Anyone:
The front-end page (I’ve cut out non relevant lines):
import wixData from 'wix-data';
import {getAdvisoryLevel} from 'backend/traveladvisory';
export async function buttonFetch_click(event) {
let counter = $w("#datasetAllCountries").getTotalCount();
var results = await $w("#datasetAllCountries").getItems(0, counter);
for (let i= 0 ; i < counter ; i++) {
var advisoryLevel = results.items[i].travelAdvisory;
var countryIso = results.items[i].isoCode;
var countryId = results.items[i].countryName;
await getAdvisoryLevel (countryIso)
.then(myData => {
console.log(myData['data']);
if (myData['data'][countryIso]) {
levelData = myData['data'][countryIso]['advisory']['score'];
} else {
levelData = advisoryLevel;
}
});
if (advisoryLevel !== levelData ) {
await updateAdvisoryLevel(countryIso, levelData)
}
}
}
The backend file:
// Filename: backend/traveladvisory.jsw (web modules need to have a .jsw extension)
import {fetch} from 'wix-fetch';
export function getAdvisoryLevel(country) {
const url = 'https://www.travel-advisory.info/api?countrycode=';
let fullUrl = url + country;
return fetch(fullUrl, {method: 'get'})
.then(response => {
//if (response.ok) {
return response.json();
// }
});
}
I don’t know why it’s even working in Preview…
It is incorrect to use both await and .then() to handle a returned Promise . In your frontend code, you are using both, which is incorrect:
await getAdvisoryLevel(countryIso)
.then(myData => {
This code is wrong. Delete the await keyword and .then() will properly handle the Promise.
Thank you again.
Indeed the code does work like that in Preview. Strange.
I’ve been looking closer at the browser log and even before running the below function there’s an error on page load - HTTP/2 403 Forbidden
The error might be as during the import from backend request upon page load?
It shows that the current user is a ‘visitor’ and not a ‘member’ as required.
However the permissions for the jsw module is set to Anyone. I’ve even re-created the module from fresh and reassigned permissions. I tested it with multiple browsers with the same result. I’m logged onto the Dashboard as Owner.
From what I see the individual Dashboard pages does not require additional permissions?
To keep it simple for testing purposes I’ve removed everything else from the function. All it does is call the backend function. I’ve even opted to hardcode the countrycode. It works in Preview exactly as expected but produces the same browser console error in Live (HTTP/3 400 Bad Request) but it seems the permission issue to import the backend module is the problem.
export async function buttonFetch_click(event) {
var newData = await getAdvisoryLevel ('ZA');
console.log(newData);
}
}
To help identify the issue, try calling a simple backend function that just returns a hardcoded value. This way at least you’ll know if there’s an issue with the frontend/backend interface.
One thing to keep in mind is that when running in Preview, you are running as admin and not as a visitor.
I’m stumped.
I’ve come to realise the issue is about more than just a specific function or even a specific backend module.
With the browser console open (tested in Firefox & Edge) I get the same error when ANY dashboard page is loading. These are custom dashboard pages I created in the editor of course. Not all of these pages import a backend module
Hi @yisrael-wix
To test the backend problem I’ve now created a simple page - visible to anyone. I’ve also created a new backend module - using the default sample multiply function. It produces the expected result in Preview but fails in Live. You can see it here: https://www.wewillnomad.com/multiply
I’ve also created an exact copy in the Dashboard to see if it changes the may permissions are handled. Same result. It still seems as if the Dashboard user is seen as a visitor and the backend modules seem to require a ‘member’ role - even though the permission is set to Anyone.
Front page:
import {multiply} from 'backend/multiply';
$w.onReady(function () {
});
export async function buttonFetch_click(event) {
var calculation = await multiply($w('#input1').value,$w('#input2').value);
$w('#textResult').text = calculation.toString();
}
Backend Module:
export function multiply(factor1, factor2) {
return factor1 * factor2;
}
webmodule permissions:
Please report bugs to Wix Customer Care to ensure timely handling of your problem. They track and monitor all incoming issues and will escalate bugs to product teams.
Thank you. After my last post here I did log a call with Wix support and today the problem was resolved. Suddenly all my backend modules are working again.