Copying data between datacollections

I have two datacollaections. one is users and another one is for books. I would like to copy the bookID field content from the book datacollection to the booknumber into the user datacollection when pressing a button or other way when submitting a form.

Hi,
You can use queries, update and insert. create an onClick event for the submit button, use query to get the user you want to update , then update it with the BookID and finally insert the data from the form.
The code should basically look like this:

export function submitBtn_click(event) {
	let data = $w("#text1").text;
	let bookID = $w("#text2").text;
	wixData.query("users")
		.find()
		.then((results) => {
			let toUpdate = {
				"_id": results.items[0],
				"bookNumber": bookID
			};

			wixData.update("users", toUpdate)
				.then((results) => {})
				.catch((err) => {
					let errorMsg = err;
				});

		})
		.catch((err) => {
			let errorMsg = err;
		});

	let toInsert = {
		"data": data
	};

	wixData.insert("books", toInsert)
		.then((results) => {
			let item = results; //see item below
		})
		.catch((err) => {
			let errorMsg = err;
		});

}

If that doesn’t answer your question please clarify the case.

Hello there… thanks for the code. I used it in my website. But I am getting some problems. Please please help me out. Here is the screenshot of the form I am using. From this form, the data is collected to a collection called “booking” .


Once the “book now” button is clicked , I want the vendor email, name, id to be saved in the collection called “booking” too. These 3 information are saved in another collection called “users”. The form is in a dynamic page which is generated using the ID from the “users” collection.
I used the following code…

export function bookButton_click(event) {
let bookedVendorName = $w(“#businessName”).text;
let bookedVendorId = $w(“#vendorid”).text;
let bookedVendorEmail = $w(“#vendoremail”)
wixData.query(“users”)
.find()
.then((results2) => {
let toUpdate = {
“_id”: results2.items[0],
“bookedVendorId”: bookedVendorId,
“bookedVendorEmail”: bookedVendorEmail,
“bookedVendorName”: bookedVendorName

        }; 

        wixData.update("booking", toUpdate) 
            .then((results) => {}) 
            . **catch** ((err) => { 

let errorMsg = err;
});

    }) 
    . **catch** ((err) => { 

let errorMsg = err;
});

let toInsert = {
“bookedVendorEmail”: bookedVendorEmail,
“bookedVendorName”: bookedVendorName,
“bookedVendorId”: bookedVendorId
};

wixData.insert("booking", toInsert) 
    .then((results) => { 

let item = results; //see item below
})
. catch ((err) => {
let errorMsg = err;
});


When I used this code, it was saving the vendor id, name, email. But while saving the email in the database it gave parsing error.

After that I connected the user input fields of the form to the “booking” collection. Now it is saving information from the form only, not the vendor id, name and email…
Please please please help me out…