How to move data from one database collection to another

Hi there! I have a great idea but struggling with how to make it possible.

On the website I created to database collections: before verification and after verification .
Here is the idea description:

Step 1: Users submit their information through user input fields. Right now it successfully goes to before verification database . This information is available in the form on the dynamic admin page and not to other users before verification. I successfully made this step!

Step 2: Admin should make sure that the information from the user is correct. Once he clicks the ā€œapprovalā€ button I want the information to transfer from before verification to after verification database that sends information to the dynamic page available to all users. I donā€™t know how to complete this step.

Problem:
Before verification and after verification databases have read&write settings. Right now the admin page takes the information from before verification database . I created the submit button and connected it to after verification database . Unfortunately, when the admin clicks the approval button (meaning submit this information to after verification database ) it creates the blank row in the after verification database and do not move the information from before the verification database.

Can someone tell me how to fix the issue?

2 Likes

Hi,

You can solve this problem by writing code.
In the onClick event get the current item, and then insert it to the after verification collection:

export function verify_onClick(event, $w) {
	let toInsert = $w("#dataset1").getCurrentItem(); //gets the current item
	wixData.insert("verifiedCollection", toInsert) //inserts the item
	.then( (results) => {
		console.log("done adding item")
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );
}

Hi Ido,

I copied your code and I have the following problems:

  1. parameter ā€˜eventā€™ is never used
  2. Missing semicolon
  3. parameter ā€˜resultsā€™ is never used
  4. errorMsg is unread

Do I need to change ā€œ#dataset1ā€ to the name before verification database?

Hi,

None of these errors should affect the functionality of the code.
Change #dataset1 to the name of your dataset that holds the current item.

Quick question regarding this code. Is there a way to specify you only want certain items from the first collection to be transferred to the second one? A way without adding all of them? Thanks!

Hi Ido,

Unfortunately it doesnā€™t work. Here is what I have:

Dataset 1 name: Enroll_provider
Dataset 2 name: Providers
I linked the approved button to Providers database with ā€œsubmitā€

here is the code

import wixData from 'wix-data';

export function button3_click(event, $w) {
	let toInsert = $w("Enroll_provider").getCurrentItem(); //gets the current item
	wixData.insert("Providers", toInsert) //inserts the item
	.then( (results) => {
		console.log("done adding item")
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );
}

When I click approve button, nothing happens. Can you please tell me what am I doing wrong?

Hello,
I still need your help and recommendation.

Slava,

Only add the dataset for the Enroll_provider collection.

You should disconnect and remove the dataset for ā€˜Providersā€™, since we are accessing it directly with the code.
Also, it is not possible to call insert() on a dataset

Change the names as follows, note that collections and datasets are two different entities:


 let toInsert = $w("#replaceWithDatasetName").getCurrentItem();
 	wixData.insert("replaceWithCollectionName", toInsert)

Ido,

I did the exact same thing you told me and it still does not work. Sorry if I am a problem.

Database 1: Enroll_provider. The page that loads all the items from the Enroll_provider database uses dataset ā€œEnroll_provider Itemā€
Database 2: Providers

The button that I click to transfer data has id: verify and onClick event name: verify_onClick

I deleted the dataset for ā€˜Providersā€™ from the page.

After I included this code the following error exists: TypeError: $w(ā€¦).getCurrentItem is not a function

import wixData from 'wix-data';

export function verify_onClick(event, $w) {
	let toInsert = $w("#Enroll_provider Item").getCurrentItem();
 	wixData.insert("Providers", toInsert)
	.then( (results) => {
		console.log("done adding item")
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );
}

Hello Slava,

The dataset name is incorrect:
let toInsert = $w(" #Enroll_provider Item").getCurrentItem();
Change to:
let toInsert = $w(" #Enroll_provider ").getCurrentItem();

Hello Ido,

Sorry, it still says: TypeError: $w(ā€¦).getCurrentItem is not a function

Hi,

Do you have a dataset named ā€œEnroll_providerā€ on your page?
Is it set to read or read & write?

Please share your editor url here.

Hi,

I do have a dataset ā€œEnroll_providerā€ with read&write mode on the page

1 Like

Slava! Iā€™m so glad you asked this! This is a great idea, and Iā€™m doing something similar. Thanks for being a ā€œproblem,ā€ so that I donā€™t have to be one! LOL!

Hello Ido, how to do it with same Collection?
Just change name of Collection is nowt worksā€¦
I need a duplicate item button in same collection

Hey, any solution to do it in same collection? its work fine when you add to another collection, but how to create duplicate in same collection?

Did anyone ever get anything to work?

Haha donā€™t think so, unfortunately

Hello, I have the almost the same requirement as here, but quite didnā€™t worked for me. I have a huge database collection, and I have a search bar to retrieve those data in a repeater. But, in my case, I just want to move the data that I retrieved in a repeater using button to the next database, and let other remains within the same old collection. I used this code, and I found that only the very first row of data gets transferred into another database, which in my case should be modified. Any helps would be highly appreciated!!

1 Like