Help with Wix beforeInsert Hook

I am trying to query a Database with another Database before an entry is made.

I have this code

import wixData from 'wix-data';

export function ArrivalsDB_beforeInsert(item, context) {

wixData.query("ArrivalsDatabase")
    .eq("fileNumber", item.fileNumber)
    .find()
    .then((results) => {
 let toUpdate = {
 "_id": results[0]._id,
 "title": results[0].email
        };
        wixData.update("ArrivalsDatabase", toUpdate)
    })
    .catch((err) => {
 let errorMsg = err;
        console.log(errorMsg);
    });

}

I have 2 Databases Arrival1 & Arrival2

  • Arrival2 contains 100’s of rows and will be used by Team A

  • Arrival1 will be regularly updated by Team B

  • Both Arrival1 & Arrival2 have the same column fileNumber

  • Arrival2 has the following columns email, name, internal id (Arrival1 does not have these)
    Basically I am trying to see if the fileNumber Entered by Team B in Arrival1 matches a fileNumber in Arrival2.

If it does match I want the email from Arrival2 to be inserted into Arrival1

My Datahook is getting this error:

Hook beforeInsert for collection ArrivalsDB result ignored! Expected hook result to resolve to an object, but got [Undefined]
TypeError: Cannot read property ‘_id’ of undefined at ArrivalsDB_beforeInsert._wixData2.default.query.eq.find.then.results (/user-code/backend/data.js:10:22) at run (/elementory/node_modules/core-js/modules/es6.promise.js:75:22) at /elementory/node_modules/core-js/modules/es6.promise.js:92:30 at flush (/elementory/node_modules/core-js/modules/_microtask.js:18:9) at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickDomainCallback (internal/process/next_tick.js:218:9)

I tried using this:

   .then((results) => {
 let Item = results.items[0];
 let toUpdate = {
 "title": Item.email
        };
        wixData.update("ArrivalsDatabase", toUpdate)
    })
    .catch((err) => {
 let errorMsg = err;
        console.log(errorMsg);
    });

}

But now I am getting this error:
Hook beforeInsert for collection ArrivalsDB result ignored! Expected hook result to resolve to an object, but got [Undefined]

What does it mean by ‘Expected hook result to resolve an object but got Undefined’
What Object is it referring to ?

Should I be using afterInsert Hook in this case ?

Hi shan,

You are getting the error message because the beforeInsert() hook is not returning anything. It is supposed to return the modified item. See the beforeInsert() API .

Good luck,

Yisrael

Hey Yisrael,

Could you point out what I’m doing wrong here according to my situation explained above

 
export async function SurveysSent_beforeInsert(item, context) {
 
  wixData.query("ArrivalsDatabase")
  .eq("fileNumber", item.fileNumber)
  .find()
  .then((results) => {
 let Item = results.items[0];
 let toUpdate = {
 'title': Item.email
    };
    wixData.update("ArrivalsDatabase", toUpdate);

  })
 return item;
}

Hi,

this one looks OK from a perspective of a database hook. Or there is some other error, like missing fields or query not returning any items.

However, the update will fail. The second argument of update operation must be the item to be updated (that is the original item with the changes applied, not just the field to be changed).

More like:

let Item = results.item[0];
Item.title = item.email; // or is it Item.email ?
wixData.update("ArrivalsDatabase", Item);