onClick command to insert data from one dataset to another

Hi guys,
I want on the click of a button to take info from one dataset and copy it into another dataset.I.e: When a logged-in user is in a page describing a course.I want the onClick action to :

  • Copy the course_id from the courses dataset

  • Insert it into a blank field named Course_id in the profile page of the logged-in user .
    Somehow, I keep missing the target :wink:The idea is simple: I want to offer a simple way of letting my users update their profile with courses they are interested in.Anybody been there before?THXPS Am I the only dufus that sees the cursor do a Home command when pressing the spacebar in this forum???

Hello?

This is the code, just in case it would generate an answer…
The logic I used is:

  1. Make sure CurrentUser is a member

  2. Use an onClick to trigger the action required

  3. Query the _id of the current item (mandate number) from the Postes database

  4. 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});

I have the same issue with the home . I am trying to follow your code to be able to reference a subset of one dataset and insert it to another dataset. Without any login. Glad you got it to work .

Hi Charles,
Good luck following my code, I’m still unable to make it work. Still hoping for some help from Wix :frowning:

I am looking at this post. https://www.wix.com/code/home/forum/community-discussion/sending-dataset-items-to-a-db-solved

Maybe it will work

O, crossing fingers, have had success using the code I linked to. I have not tried it in live yet.

Hi louvail2:
Some things for you to consider.

  1. If you want to update an existing record in a data collection you need the _id field for that record.
    check this out: wix-data - Velo API Reference - Wix.com
    So what you may want to do is read the record from the data collection first (unless you know the _id) then
    update the record you retrieve with the ‘Mandat’ value.

Now for folks to help you solve your problem if you share the schema for each of your data collections and how they are connected that will help. Also a screen shot and the URL for the page you are trying to get to work will be good along with a description of what you are trying to do.

My brief assessment is that you are not using the promises for the data api correctly (in addition to my comment about the _id above). But cant say for sure until there is more data to go on.

Thanks stcroppe,your comments are appreciated and will probably help me.Tired of waiting, I went on with other things to do, but I’ll come back to it and if I have a soliution, I’ll share it with the Forum members…stay put LOL

No Problem, I am a developer and can probably help you get to a solution if you still need help. Hence the post.

Steve

I’m baaack!! I think putting our heads togheter may help the community for I know I’m not the only one with an issue with an export function, onClick to Submit 2 tables at once.

So here we go:
A form filed with many inputs goes to one dataset called Candidates. All but for one dropdown that should go to another table called Subscriptions.

The reason is that it will be possible for one candidate to populate the field more than once, hence the need for a table that will gather all the applications, from all the candidates.

So I am able to submit to the Candidates table, no problem, and I added a code to the same button hoping that the onclick will populate the Subscription table…

Here’s an attempt, don’t laugh…

let link-candidats-update-Id= Candidats Update ID;

// create an item
const toInsert = {
“_id”: link-candidates-update-Id, //field of the userID in the Candidates table
“email”: userEmail
};
// add the item to the collection
wixData.insert(“Subscription”, toInsert)
. catch ((err) => {
console.log(err);
});

export function button8_onclick() {
let Subscriptions = {
“retenuLrh”: $w(“#dropdown6”).value, //fileds in the Subscriptions database
“CandidatesUpdateID”: $w(“”).value,
“First name”: $w(“#input3”).value,
“Last name”: $w(“#input4”).value,

}; 
console.log("Subscriptions" + JSON.stringify()); 
wixData.insert("Subscriptions") 

}

Any idea?

Thanks

Hi Louval:

OK - I think you need to consider your data models in the context of your page requirements.
You seem to want to set up a many to many relationship between Candidates and Subscribed to Courses.

Many Candidates able to choose many Subscribed classes.

This will typically require three data collections

  1. Candidates
  2. Subscriptions
  3. Joining Table mapping Candidate to Subscription

So the page will essentially have a repeater showing a cards each representing the Candidate. On the card the drop down will show all available subscriptions from the subscriptions data collection
Also on the card will be another list (repeater) containing all of the Subscriptions from the joining table where the record contains the Candidates id.
When a drop down item is selected the id for the item from the Subscription table (this would be the Drop down item value) and the id for the Candidate on the card are combined into a new record in the join table.
After the join table is updated the Subscriptions list gets updated.
You could do a reverse where the list of subscriptions has a delete option that allows you to unsubscribe.

I think this is what you are trying to accomplish. I will try to knock together a quick example for this if you concur with this summary to show you how it is done.

Steve

Hi Steve, thanks for your time on this, and yes, this is what I have been trying to accomplish, structurally speaking. Since the Candidates table cannot populate more than one entry per field for a given candidate_ID , I concluded I needed a 3rd table where all the subscriptions would be logged, and retreivable through a repeater in the Candidate profile page .

Candidates can create an account (stored in the Candidates table) and modify their personal profile on the CandidateUpdateID page. Candidates can subscribe to applications on their own and this is logged in the Candidates table, in the field subscription .

I also have a hidden page called Candidates entry form , from where I populate the same Candidates table. This table has a lot more fields than the user (candidate) has access to. From this page, I can add potential candidates on my own in the Candidates table and suggest subscriptions for each, from a dropdown menu. This is the item (dropdown) that populates the Subscriptions table.

The Subscriptions table has 3 simple fields : CandidateUpdateID , subscription (from Candidates table) and suggestions , from the Candidates entry form dropdown.

In trying this, I was confronted to 2 problems:

1- I cannot submit the candidate’s info ( first name, name, email etc.) into Candidates table AND the dropdown data, Subscriptions at once;
2- I cannot find a way to use the candidates_ID in order to link the 3 tables together.

I hope I’m clearer this time :slight_smile:

Thanks again

OK - I think I understand. One challenge is that you cannot nest repeaters at the moment, as far as I know. So getting the result you are looking for may require a slightly different page experience. I’ll mock something up and show you the code etc. with a link shortly.

Steve

Thanks a million Steve. I hope I’ll honor your efforts appllying successfully your code :slight_smile:

PS I think we’re not in the same time zone, must be very early for you
Louis

Hi Louval:

Does this do what you are looking to try to do?
https://stcroppe.wixsite.com/steveonsoftware/wix-many-to-many-contact-cards

Steve

Hi Steve, thanks for the hard word. Unfortunately, this is not what I’m trying to do. Especially regarding hover functions.
l’lI try to imge my explantions and going chronologically:

1 - People can sign in and create a personal profile. They become then, Candidates. Thir info is obviously logged in the Candidates table.
2 - after sign up, they can acces a profile page called Candidates Update. This generates a new userID updated by the candidate.


3 - I also have a data entry page called Cadidates signup form. This is where I have a challenge to upload info in 2 tables at once.


PS it is normal if you find the input field strange… they’re in French LOL

Thanks again Steve

Hi Louvail:

Try to ignore the hover functionality. That could be a click or anything else.

The main point of the example is to show how to make use of two data collections and join them using a bridging collection.

The way the example works is when you hover over the persons name (this is a repeater) it sends a message from the onClick to change the profile card and setFilter on the data set that drives the hobby list.

The hobby list is one data collection, the members are another. With a member selected by hovering over the list - you can click on the drop down which lists all hobbies and when you select one it is automatically added to the bridging data collection and the repeater listing hobbies for the member is updated. You can also delete hobbies from the list for each user and the applicable record will be deleted from the bridging data collection. Then the repeater will be reset.

If you look at the code for the page you will see how the two data collections are joined by looking at the dropdown onChange function and the delete onClick function.

I have also provided a view of each data collection to show what fields are needed to make this work.

If you want me to look at your specific page you will need to contact me directly using the information on my profile.

Cheers