Data Collection A:
ID,name,email --> has values
Data Collection B:
ID,name,email --> need the same values from data collection A.
Please help with any simple solution for this
Important forum update
This forum is migrating to one unified Wix community forum starting July 26th, and will be read-only during the process.
Wishlist Page is the official platform for requesting new features. You can vote, comment, and track the status of the requested features.
Thank you ezrasmithdesign for your help and example code, and thank you J.D. for those links. I was able to get this all to work. I'll paste my example below for anyone to reference in the future:
CollectionA (collection ID = CollectionA)
ID (field key = _id)
Title (field key = title)
URL (field key = url)
CollectionB (collection ID = CollectionB)
ID (field key = _id)
Title (field key = title)
URL (field key = url)
You'll need to experiment with your Collections Permissions based on your site and specific scenario, but here's what I did on my end to get it to work the way I wanted:
Collection A Permissions:
Site member author: can view content
Site member: can add content
Site member author: can delete content
Site member author: can update content
Collection B Permissions:
Anyone: can view content
Site member: can add content
Site member: can delete content
Site member: can update content
In this example, let's say you have a page on your site called Page1 and a container box for site members to submit some info (their Title, their URL, and a Submit button). Using a dataset on Page1, you connect your Title field, URL field and Submit button to your CollectionA collection, WRITE mode. When a site member hits Submit, this channels their info into CollectionA.
You also decide to put another container box with the same fields, but an Update button instead.
So we have:
SUBMIT container box
Title field (field key = title) = #input1
URL field (field key = url) = #input2
Submit button = #btnSubmit
UPDATE container box
Title field (field key = title) = #input3
URL field (field key = url) = #input4
Update button = #btnUpdate
On your Page1, use this code:
import wixData from 'wix-data';
export function btnSubmit(event) { let toInsert = { "title": $w('#input1').value, "url": $w('#input2').value }; wixData.insert("CollectionA", toInsert) .then( (results) => { let item = results; }) .catch( (err) => { let errorMsg = err; }); } export function btnUpdate(event) { let toUpdate = { "_id": 'ID', "title": $w('#input3').value, "url": $w('#input4').value }; wixData.update("CollectionA", toUpdate) .then( (results) => { let item = results; }) .catch( (err) => { let errorMsg = err; }); }
Next, in Dev mode, go to the Databases panel > click the 3 dots next to CollectionA > Add/Remove Hooks > add afterInsert, and add afterUpdate.
This will launch a data.js tab. On this tab, use this code:
import wixData from 'wix-data'; export function CollectionA_afterInsert(item, context) { let toInsert = item; wixData.insert("CollectionB", toInsert) .then((results) => { let item = results; }) .catch((err) => { let errorMsg = err; }); } export function CollectionA_afterUpdate(item, context) { let toUpdate = item; wixData.update("CollectionB", toUpdate) .then((results) => { let item = results; }) .catch((err) => { let errorMsg = err; }); }
Now, whenever, a site member submits info in the Submit box, it will channel into CollectionA and then automatically populate in CollectionB too. And if the site member decides to update info in the Update box, this will successfully overwrite the existing item/line with their updates. It won't create new items/lines.
If the data in the two databases are identical, you should just set the read permission to "Anyone" ("Anyone can view content") and then add and update permissions to "Site member author." That way only site members can update and add content, but anyone can view it without fussing around with moving data between two databases.
But, if you still think that copying data between databases is the best option for you, here is some code I worked up (not tested):
Initial insert:
import wixData from 'wix-data'; //set up trigger when form submit button is clicked export function formSubmit(event) { //get content from Form: let toInsert = { "title": "Title.", //use $w("Title Input Field from From").value, "Image": "Image", //connect to form field ^ "url": "Url" //connect to form field ^ }; //insert to A: wixData.insert("databaseA", toInsert) .then((results) => { let item = results; //see item below }) .catch((err) => { let errorMsg = err; }); //insert to B wixData.insert("databaseB", toInsert) .then((results) => { let item = results; //see item below }) .catch((err) => { let errorMsg = err; }); //notice that both insert function use the same toInsert object defined above }
For the hooks, open the "Databases" section of the "Site Structure" panel, then click the ellipses next to Database A. Then add an "After Update" hook that would look something like this:
//In the data.js file (tab) import wixData from 'wix-data'; export function DatabaseA_afterUpdate(item, context) { let toInsertB = item; wixData.insert("databaseB", toInsertB) .then((results) => { let item = results; //see item below }) .catch((err) => { let errorMsg = err; }); }
You may run into issues because of the permissions on the databases, so if it doesn't work right away check your permissions.
Hi, did anyone in this thread get this to work and might have any example code they'd be willing to share? Or might be able to help with my example below?
I'm trying to test a simple example on my own. Let's say:
CollectionA (collection ID = CollectionA)
ID (field key = _id)
TitleA (field key = titleA)
ImageA (field key = imageA)
urlA (field key = urlA)
CollectionB (collection ID = CollectionB)
ID (field key = _id)
TitleB (field key = titleB)
ImageB (field key = imageB)
urlB (field key = urlB)
A site member enters some data into a form on my site --> the data populates into CollectionA --> I'd like the identical data to then immediately get auto-populated into CollectionB (including the identical ID that was created in CollectionA). And if the line gets updated/modified in CollectionA, then those updates/modifications will overwrite the line in CollectionB (as opposed to generating a new line).
J.D. posted some nice resources which are great. But I'm confused if I am supposed to be attaching inserts and hooks to both collections, or just CollectionB. That's why it'd be really great to have a simple example code (for this type of scenario) if anyone has one!
I've been looking for the same soluction but it's not working what iḿ trying
import wixData from 'wix-data'; export function Members$PrivateMembersData_afterInsert(item, PrivateMembersData ) { let hookContext = PrivateMembersData; // see below // some changes to the received item return item; } export function testes_rapha$mytable_Insert(item, mytable); export function Members$PrivateMembersData_afterUpdate(item2, PrivateMembersData ) { let hookContext = PrivateMembersData; // some changes to the received item return item2; } wixData.update("testes_rapha$mytable", item2) .then( (results) => { let item = results; //see item below } ) .catch( (err) => { let errorMsg = err; } );
Has this issue been taken up by the wix team?
I want to duplicate the services collection as I want to add more fields to the Booking service
More details are needed. When do you want this copy to take place? Once a new record has inserted to collectionA? Once an existing record in collection A has been updated? Once a day? On button click?