hooks updating field in other collection

Hi
Does anybody knows code for changing value of field in collection when other has changed.

i stucked with updating value of “numer” in collection “zakwaterowanie” to collection “listagosci” pokoj. g2 is refernced with title of listagoscie therefore let gosc=item.g2.
Console.log shows me good values but error shows " Hook afterUpdate for collection zakwaterowanie result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [Undefined] "

thanks in advance

export function zakwaterowanie_afterUpdate(item, context) {
let gosc = item.g2
let id=gosc._id
let pokoj = item.numer
// console.log (“hoooookspokoj”)
// console.log(pokoj)
// console.log(id)
let toUpdate = {
“_id”: id,
“pokoj”: pokoj,
};
wixData.update(“listagosci”, toUpdate)
.then( () => {

return item;
})}

Hi,

There is no need to add an update() function inside the hook, the function know to get the item that has changed and return an update one directly to the collection.

The afterUpdate() hook function runs:

  • The update() function is called- at the page code in your site not inside the hook

  • An action is performed on a dataset that updates an item from the collection.

  • An item is updated using the Content Manager.

If there is still a problem, please send the URL to your site and specify the name of the collection , and the name of the page that call to the update function().

Best,
Sapir

But i want when update item in collection1(hook) update item in collection2 and want o use hook. Not the same collection.
i thought to update other collection in backend, in collection directly not in dataset . I can do it on site but code is quite slow there and wanted to do it in backend and i thought everything should be there. I will try than on site. But whats the use of hook than if anyway code is on page?

Hi,

I didn’t understand previously that you wish to update another database, if so its possible to do it in the hook.
Pay attention that the reference filed in the item include the id and not all the object, as a result, in order to get the id of the reference item you just need to write

let id =item.g2 (g2 is the reference field)

*You can do console log to the item and you can see that you get an object that the reference filed include the id.

Beat,
Sapir

i already checked it and item.g2 is a scope not id. In collection you see already items included not referenced id.
its from console log when i see item.g2
Line 20{ _id: ‘5339a6cc-df57-431d-a0d6-8ede5e2fc0c7’, _owner: ‘5c4794ca-8ac7-4193-9e61-6bf81eb53307’, _createdDate: 2018-12-09T18:09:04.421Z, _updatedDate: 2018-12-22T12:48:11.601Z, imie: ‘LESZCZYNSKA BASIA’, nocleg: ‘CEGLARNiA’ }

Therefore i made let gosc= item.g2
and then id =gosc._id

when its like this in console now i see variable “pokoj” that i want to put to another collection and id of the item where to put it. So seems everything is ok but then

Expected hook result to resolve to an object with an ‘_id’ property, but got [Undefined] "

There is something wrong with return but i cant find out what :((((

i tried with before update and it workes as You wrote. So is it possible return of before and after are different? But there is small question then
wixData.update(“listagosci”, toUpdate) i also tried with save

its saving me only id and pokoj and delete rest of values. Is it possible to update only those items and left rest untouched or i have to put all items into “toupdate”

Thanks for helping

ok i read in API-Date save behave like update so i have to put all item. So fundamental question is it really faster and safer using hooks. Because i undestand what are they for but practically when You for example make websites with many datas You use hooks ? maybe there is not much difference when i do it on page or in hooks.I’m asking because i need to do few such operations and dont know is it make sense. Maybe on page will be the same.

Chris

Hi,

The advantage of using Hook functions is to avoid duplicate code that has the same functionality.
This means that whenever you wish to change your code you will only have to do it once.

Best,
Sapir