This is the code, just in case it would generate an answer…
The logic I used is:
-
Make sure CurrentUser is a member
-
Use an onClick to trigger the action required
-
Query the _id of the current item (mandate number) from the Postes database
-
Insert current value from the Postes database (mandate#) in the Mandat field of the currentuser (inside the Candidats database)
PS the Mandat field that is supposed to be updated is a Referral field.
If I use the toSave command, it adds another line in the Candidats databe with nothing else than the let toSave = “info” in the Mandat field and it says Reference is broken
$w.onReady(() => {
if (wixUsers.currentUser.loggedIn) {
$w(“#button4”).label = “Terminer la session”;
$w(“#button8”).hide();
$w(“#button9”).label = “SOUMETTRE VOTRE CANDIDATURE”
} **else** {
$w("#button4").label = "Ouvrir une session";
$w("#button9").hide();
$w("#button8").label = "SOUMETTRE VOTRE CANDIDATURE"
}
});
export function button4_onclick() {
// user is logged in
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixLocation.to(“/logout”);
wixUsers.logout()
.then(() => {
// update buttons accordingly
$w(“#button4”).label = “Ouvrir une session”;
$w(“#button5”).hide();
//redirect home
wixLocation.to(“//home”);
});
}
// user is logged out
else {
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin({
“mode”: “login”,
“lang”: “fr”
})
.then((user) => {
userId = user.id;
return user.getEmail();
})
.then((email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query(“Candidats”)
.eq(“_id”, userId)
.find();
})
.then((results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
“_id”: userId,
“email”: userEmail
};
// add the item to the collection
wixData.insert(“Candidats”, toInsert)
. catch ((err) => {
console.log(err);
});
}
// update buttons accordingly
$w(“#button4”).label = “Terminer la session”;
$w(“#button5”).hide();
wixLocation.to(/Candidats/Update/${wixUsers.currentUser.id});
})
. **catch** ((err) => {
console.log(err);
});
}
}
export function button9_onclick() {
wixData.get(“Postes”, “CurrentId”)
.then((results) => {
let CurrentId = results.items[0]; //see item below
})
. **catch** ((err) => {
let errorMsg = err;
});
}
let toUpdate = {
“mandat”: “”,
};
// add the item to the collection
wixData.update(“Candidats/wixUsers.currentUser.id”, “Mandat”)
. catch ((err) => {
console.log(err);
wixData.update("Candidats", toUpdate)
.then((results) => {
let CurrentItem = results; //see item below
})
let errorMsg = err;
});
wixLocation.to(/Candidats/Update/${wixUsers.currentUser.id});
