Update Repeater to Collection

Update 1st April 2020

The image below is sourced from a National Flags Collection and is displayed on a dynamic page.

Dear Community, how can i get three elements shown in the repeater inserted into a different collection by clicking on the button titled, Add to Collection Item?
Note: only one of the repeater container items would be selected for update e.g. United Kingdom, therefore United Kingdom, Europe and the associated image for the UK flag would be updated into “myCollection” if i can get the code to work

Field keys and elements used

  1. country - same fieldkey in both collections - page text element #text196
  2. continent - as above - page text element #text197
  3. countryFlags (image) - as above -page element is #image84

The collection used to display repeater items is called and from “NationalFlags”
The collection that i wish to update the three fields above is called “myCollection”

If as previously explained in other posts I need to query the repeater collection (National Flags) first, how should this be coded please? then how should i proceed.

I have struggled to code the update code also and only managed to delete a row and insert one field key, since then i have lost the code while trying other methods

I have reached out to Wix Arena previously and for this code I cannot afford £k’s as quoted for this code, so this community help is much needed.

Can anyone assist with code please?
Thanks
Adam

Please do not duplicate posts and bump old forum post from 2018.
https://www.wix.com/corvid/forum/community-discussion/saving-data-from-repeater?origin=member_comments_page

No Thread Hijacking. Don’t post about an issue and then bump other threads with your issue.

@givemeawhisky apologies. Obviously i’m trying very hard to complete this task. I’ll stick to this thread

So what code do i need to actually be able to insert displayed data from my repeater into an existing collection item?

I have added this code don’t know if this is the right approach, also not sure which element or button the onClick event should be in this code?

Thanks and Regards
Adam

I have to admit that I’ve read over your posts several times and still don’t understand what you are trying to do.

You have a repeater that displays countries and their flags. Why are you trying to insert them into another collection? What is your second repeater for?

In short, what is it exactly that you are trying to do? Please explain in simple terms as I think I’m having a “dense” day.

Good morning Yisrael,

Ok hopefully this will be clearer. First of all I have a repeater that is shown on a dynamic page. This repeater is linked to a collection called National Flags. (as per the very first screen shot that shows three different countries in a repeater)

There is a search facility above the repeater to find a specific country and when the country is found this displays the country that shows, country, continent and the national flag as an image.

Inside the repeater is a button. It’s this button that i’d like to use to update the users choice into another collection, so that this collection shows all the records pertaining to the users current “_id” including country, continent and countryFlag image

Does that help at all?

What is the purpose of the dynamic page? Why a dynamic page ?

After the user has selected a country, you then need to query the second collection to find the user’s data. You can update the fields in the returned item using the selected country, and then update() the item in the collection.

I hope I understood correctly what you are trying to do.

Also, please observer the guidelines and provide code in code blocs, and not in screenshots.

@yisrael-wix i also wondered if i’d need this code block to get the repeater items into the second collection

$w.onReady( function () { $w(“#repeatedContainer”).onClick( (event) => { let $item = $w.at(event.context); let clickedItemData = $item(“#myDataset”).getCurrentItem(); } ); } );

The main thing is for my is that i don’t think i’m coding the update code correctly or any of it to be honest, to transfer the three repeater items into the second collection as an update (and just to be clearer the second collection essentially holds first name, user name, location, age etc. data)

Thanks and Regards
Adam

Update 01/04/2020 20:13hrs

I’ve tried to query and insert and also get errors, i’m not sure what else to try now. I thought taking values from the repeater and updating into a different collection with button in repeater would have been easy :slight_smile:


code block 1
followed by code block 2 from line 18

or maybe as @roi-bendet suggested i need to use something like this, which suggests that i can pull information from the repeated items. Perhaps then i can use those values to update “myCollection” ?

forEachItem( )

Runs a function for each repeated item.

Description

Use the forEachItem() function to run a function on all of a repeater’s repeated items. You can use the callback function to update or pull information from all of the repeater’s repeated items.
Thanks and Regards
Adam

What do we think folks, is this really as hard as i’m making it look and sound. Obviously it is for me but you guys really know this code inside out

Some good progress here thanks to @code-queen

I now have one field being populated in the second collection from the repeater as shown here, which is the continent - very happy thanks CodeQueen

I really now need to get the country and countryFlag into the collection. Then i can hopefully add getCurrentUser so this info is assigned to the right item / row in the collection

I’m not sure what your trying to achieve but if you want to store the item from the repeater (whose button is clicked) in another collection then probably do something like this:

export function button46_click(event) {
    let $item = $w.at(event.context);
    let clickedItem = $item("#myDataset").getCurrentItem();
    let toInsert = {
        title: clickedItem.title,
        fieldOne: clickedItem.fieldOne,
        fieldTwo: clickedItem.fieldTwo
    };
    wixData.insert("myCollection", toInsert);
}

We scope the repeater item which has been clicked and get the entire item from the dataset on the page. After this I can write the code to insert the item into a new collection.

In the above code “clickedItem” is the entire Item Object so you can use dot notation to get the appropriate fields.

@Shan thanks that works pefectly. Now i’ve just got to find a way to make sure those inserts go into the current collection item and not create a new row. As this is on a dynamic page i think this should be doable

I’ve tried to add a query as follows but get an error
TypeError: Error.captureStackTrace is not a function. (In ‘Error.captureStackTrace(t,m)’, ‘Error.captureStackTrace’ is undefined)

I think i’m almost there now guys, just need to find a way to get the inserts or saved fields (have experimented a little here but can change back to let toInsert) into the same row as current user of MyCollection not create a new row

in MyCollection i already have three fields above with default text and image from setFieldValue on a previous page if that helps.

just remember that dataset1 pulls data for repeater from a different collection and ‘MyCollection’ is the collection that i want to insert or save into, on the current row / item

This is my latest attempt


Thanks as always
Adam

So even when i change let toSave or let toInsert to let toUpdate i still can’t update into the existing item in “MyCollection” database.against a current _id or fullName field in “MyCollection”

This still creates a new item even though i have the three fields populated by default values. So that the clickedItem’s should update those fields

I’m missing the right code to instruct the save, update or insert to put this fields in the right existing fields

Hi Adam !

I’ve got the same problem : https://www.wix.com/corvid/forum/community-discussion/update-item-from-a-repeater-to-a-database

Have you solved it ?

Thank you :hugs: