Hi, i am quite new working with backend. I am having an issue that once the button is clicked, nothing basically happens. Can someone could help with the code and maybe find the mistake why i cannot call code from backend? Maybe i missed some basics in both front end and backend? I also suspect some issues with backend URL address.
Frontend:
export function button1_click(event, data) {
$w("#button1").disable(); // Disable the button to prevent multiple clicks
$w("#loadingGIF3").show();
$w("#confirmed").show();
console.log("Insert content scan");
let toInsert = {
"email": $w("#emailinput").value,
"title": $w("#titleinput").value
};
console.log("Inserting");
fetch('/backend/Registration-checkuser.jsw', { // Update endpoint to match backend web module
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(toInsert)
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error('Error: ' + response.status);
}
})
.then((results) => {
let item = results;
$w("#dataset1").save();
$w("#loadingGIF3").hide();
console.log("Saved!....");
wixLocation.to(wixLocation.url);
})
.catch((err) => {
let errorMsg = err;
});
}
Backend:
import wixData from 'wix-data';
export function button1_click(request) {
const email = request.body.email;
const title = request.body.title;
// Remove spaces from the beginning and end of title
const formattedTitle = title.trim();
// Capitalize first letter of each word in the formattedTitle
const words = formattedTitle.split(' ');
const capitalizedWords = words.map(word => {
return word.charAt(0).toUpperCase() + word.slice(1);
});
const formattedTitleCapitalized = capitalizedWords.join(' ');
// Extract first name and last name from the formattedTitleCapitalized
const [firstName, ...lastNameArray] = formattedTitleCapitalized.split(' ');
const lastName = lastNameArray.join(' ');
// Create an object to insert into the "nsflMain" collection
const toInsert = {
"email": email,
"title": formattedTitleCapitalized,
"firstName": firstName,
"lastName": lastName
};
return wixData.insert("datasetname", toInsert)
.then((results) => {
return {
success: true,
data: results
};
})
.catch((err) => {
return {
success: false,
error: err
};
});
}
Thanks guys!