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)