Copy from one collection to another after update

Hello Guys,

I’m developing a website where members can upload a document. I created “members” collection which have a field called “fileUploaded” of type “document”. If registered member trying upload document, i’m saving in this field. If user accidentally updated wrong file or wanted to upload another file, i need to save previously updated file also. For this purpose i created “UserDocuments” collection which can hold “memberID” and “fileUploaded” from “members” collections.

To simplify, I need to save all the file(s) updated by members in a new collection where i filter or query by “Member ID” later. For this purpose I’m trying to use Hooks concepts on “members” collection after update. Can anyone help me on how to insert record into “UserDocuments” collection on updating members collection “fileUploaded” field.

I need to insert into “UserDocuments” collection only if “fileUploaded” field is updated in members collection.

import wixData from ‘wix-data’;

export function members_afterUpdate(item, context) {

// Code sample needed : to insert into UserDocuments Collection if fileUpload is changed

}

Hi,
Maybe you should try another approach, instead of connecting the upload to the data base, create an onClick event or any other event you can use, use query to check whether the user already uploaded an item, based in the answer use insert to insert the data to the relevant database.
check out the following:

Good luck

Thanks Or. I tried below approach, but it is not working.

export function buttonUpload_click() {

let userId;         
    let filename; 
    let isValid = 1; 
    
	userId = wixUsers.currentUser.id;	 
	filename = $w("#uploadButton1").value; 

	const toInsert = { 
                    "memberId": userId, 
                    "memberDocument": filename, 
					"isCurrent": isValid 
                }; 
	 // add the item to the collection 
                wixData.insert("memberDocuments", toInsert) 
                    .catch((err) => { 
                        console.log(err); 
                        	console.log("Error Occured by Admin"); 
                    }); 

}

Any idea how get uploaded document in JavaScript to insert into collection?

You didn’t use query to insert the data to the right database…
check whether the member already uploaded a file, if he did insert to the “userDocuments” database and if he didn’t insert to the “members” database.

I use this code work perfect, its only between collections and dataset, let me know if you need some help

$w.onReady(function () {
let itemObj
$w(‘#dataset1’).onReady( () => {

$w(‘#button4’).onClick(()=>{
itemObj = $w(‘#dataset1’).getCurrentItem();
console.log(itemObj)
console.log(itemObj.title)
console.log(itemObj.make)
console.log(itemObj.model)

$w(“#dataset3”).onReady(() => {
$w(‘#dataset3’).setFieldValue(‘title’, itemObj.title)
$w(‘#dataset3’).setFieldValue(‘make’, itemObj.make)
$w(‘#dataset3’).setFieldValue(‘model’, itemObj.model)
$w(‘#dataset3’).save()

})
})

$w(‘#button4’).onClick(()=>{$w(‘#input1’).value = $w(‘#dataset1’).getCurrentItem().title})
$w(‘#button4’).onClick(()=>{$w(‘#input2’).value =$w(‘#dataset1’).getCurrentItem().make})
$w(‘#button4’).onClick(()=>{$w(‘#input3’).value=$w(‘#dataset1’).getCurrentItem().model})
})

});