wixData.update in a hook _afterInsert()

Hello,

I have an inserted line with a field named “from” in a dataset A

My requirement is to fill another field named “fromOACI” in the same dataset A from another dataset B

Dataset A:

Dataset B:

I want the hook after insert be able to populate automatically the field “fromOACI” from the Dataset A.

In my example I am expecting the field fromOACI to get the value “LECO”

Unfortunately this is not working, I get the error:
Hook afterInsert for collection flightDataset result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [Undefined]

Thanks for your help.
Regards,
Pym

export function flightDataset_afterInsert(item, context) {

wixData.query(“airportDataset”) // airportDataset = Dataset B
.eq(“airportName”, item.from)
.find()
.then((results) => {
let toUpdate = {
“_id”: item._id,
“fromOACI”: results[0].oaci
};
wixData.update(“flightDataset”, toUpdate) // flightDataset = Dataset A
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
});

Hi pierre,

i think the problem is that you are using the same id from the insert item of table B to update the record in table A. you need to take it from the results of your query

another question, is why are you copying the data? it seems you should us a reference from table A to Table B.
Table B looks like a list of airports, which table A references.

Shlomi