Help with insert data from datasets/page to database

Hi,

I can use some help with some custom on-click code.
I am not a coder and still trying to figure things out by copying code parts from Q&A of other users.

What I am trying to do:

I have a page with a “repeater” that is connected to a collection called “offers”.
The page also has members dataset connected.

I am trying to set the button on the repeater to do the following:

1.
Fetch the user email (from the members collection) and offer title (from the offers collection) and place it into a third collection (users_offers) creating a new row.

2.
Link to a dynamic page based on “users_offers” (I am using the unique ID in the users_offers collection in order to create the URL.
So, the dynamic page is actually based on a collection row that is created upon clicking the button that links to it.

Where I got so far:
1.
I managed to come up with the following code that works partially but only on “preview mode” and not at all on the live website (even after publishing the changes).
On preview mode it fetches my email and “offer title” of the first item in the repeater regardless of the offer I click on (always the same value).


import wixData from 'wix-data';
export function button4_click(event) {
let title = $w("#dataset2").getCurrentItem(title);
let loginEmail = $w("#dataset1").getCurrentItem(loginEmail);
let toInsert = {
"user_id": loginEmail,
"offer_id": title,
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
wixData.insert("Users_Offers", toInsert, options)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );

I built the dynamic page and linked it to the button but nothing happens when I click on it.

Thanks in advance.

It’s not enough to Publish the site. You also need to sync your sandbox database to the Live database:

Thank you for replaying.
I just did and nothing changed.
I think the code is not good.

So, I managed to make the code work on my live website.
I am able to pull the user email address but when trying to pull the offer title I always get the first value in the repeater regardless of which offer I click on.

let title = w("#dataset2").getCurrentItem(title);

Make sure that you are using the Repeated Item Scope in your event handler.

See the post Removal of the $w Parameter from Event Handlers for more information.

Thank you!

I have managed to pull the data for the right repeater item by using the code:

import wixData from 'wix-data';
export function button4_click(event) {

let $item = $w.at(event.context);
let title = $item("#dataset2").getCurrentItem(title);
let loginEmail = $w("#dataset1").getCurrentItem(loginEmail);
let toInsert = {
"user_id": loginEmail,
"offer_id": title,
};

The problem is that it inserting all the data to the database row instead of just the specific parameter I need (in my case it is “title” from dataset2

Update:

Success! I have added “” to the “title” and now it is working.

import wixData from 'wix-data';
export function button4_click(event) {

let $item = $w.at(event.context);
let title = $item("#dataset2").getCurrentItem("title"); // added "" here to title
let loginEmail = $w("#dataset1").getCurrentItem(loginEmail);
let toInsert = {
"user_id": loginEmail,
"offer_id": title,
};

Thank you!!

1 Like