Comparing field values in 2 databases with code

Hi. Hoping someone can help me with this one. I’ve read up on reference fields but not sure if this can work without using code.

I have 2 databases and want a field value in one of them to be determined by comparing the field values in both databases, as follows:
If Field 1 in Database A is equal to Field 1 in Database B AND Field 2 in Database A is equal to Field 2 in Database B,
Then Field 3 in Database B is equal to “3”.

How would I go about writing code for this? Thanks

1 Like

Hi,
You can simply compare the fields using query and change the wanted field using update or insert, check out the API here .

If you you are having trouble writing the code we will be happy to help you.

hi Or. Thanks for the above advice. I’ve been playing around with it but can’t get it to work. Do you have to do a separate query for each field search or can you search 2 fields with the 1 query? I was thinking of using a button on one of the pages which on clicking would calculate the new field values but then saw a previous post which advised using the afterInsert hook instead, so now I’m all confused on how to go about it.
Is there any pseudo code examples you could share to help? I can’t find any that fit what I’m trying to do.
Thanks.

Hi,
You need to match this code to your site based on the way you save the data
import wixData from ‘wix-data’;

export function BUTTON_onClick(event) {
  wixData.query('DATABBASE1')
    .eq('NAME_FIELD', name)
    .find()
    .then(res => {
    const field1 = res.field1;
    const field2 = res.field2;
    })
    .catch( (err) => {
    let errorMsg = err;
    } 
    );    
  wixData.query('DATABBASE2')
    .eq('NAME_FIELD', name)
    .find()
    .then(res => {
    const field3 = res.field1;
    const field4 = res.field2;
    })
    .catch( (err) => { 
    let errorMsg = err;
     } 
     );
 if(field1 === field3 && field2 === field4){
   let toInsert = {
   "field3":        "3",
   };
   wixData.insert("myCollection", toInsert)
   .then( (results) => {
   let item = results; //see item below
   } )
   .catch( (err) => {
   let errorMsg = err;
   } );
 }   
}

Good luck :slight_smile:

Thank you so much for this Or. Really appreciate it. I’ll play around with it now and see how I get on.

I have the same question, too. I want to compare two values in a dataset, if it’s true, a successful light box will appear, If not, this will be a failure light box . So how can I do this with wix code? (this’s a contest, the students submit their homework, and receive a score after each question.)