RESOLVED - Understanding how hooks work

Help with hooks:
I read the guide but I have difficulty understanding how hooks work:

example how afterInsert does to which insertion event I refer to, that is, if I have two innsert in the code, how do I understand which to refer to?

(item, context) what I am, I can not understand

Who helps me?
I made this summary of my code to understand

export function RedirectCerimonie_afterInsert(item, context) {
console .log('Try AfterInsert ’ + hookContext);
}

export function SaveData_click() {
let toInsert = {
“dataEvento”: $w(“#ImptData”).value,
“tipoEvento”: $w(“#ImptEvent”).value,
“nomeEventoOErimonia”: $w(“#ImputName”).value,
“code”: $w(“#ImptCode”).value,
“utenteRedirect”: $w(“#ImpuUser”).value,
“redirect”: $w(“#ImputUrl”).value
};
wixData.insert(“RedirectCerimonie”, toInsert)
.then( (results) => {
$w(“#textSaveForm”).text = “Form salvato con successo”;
$w(“#textSaveForm”).show();
})
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
});
}

Thank you

Someone who can help me? It would help me a lot. Thank you

Hey there Red,

What specifically are you having a problem with?

This won’t work:

console.log('Try AfterInsert ' + hookContext);

It should be:

console.log('Try AfterInsert ' + context);

In any case, what are you trying to find out? And what are you trying to accomplish?

Yisrael

Hi Yisrael,

my goal and understand how they work. Let’s take an example:

NOW MY CODE

function hideImput() {
setTimeout(() => {
$w(‘#textSaveForm’).hide(‘FadeOut’);
}, 1500);
}

export function SaveData_click() {
let toInsert = {
“dataEvento”: $w(" #ImptData “).value,
“tipoEvento”: $w(” #ImptEvent “).value,
“nomeEventoOErimonia”: $w(” #ImputName “).value,
“code”: $w(” #ImptCode “).value,
“utenteRedirect”: $w(” #ImpuUser “).value,
“redirect”: $w(” #ImputUrl “).value
};
wixData.insert(“RedirectCerimonie”, toInsert)
.then( (results) => {
$w(” #textSaveForm “).text = “Form salvato con successo”;
$w(” #textSaveForm ").show();
hideImput()
})
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
});
}

it is possible to change it with

export function RedirectCerimonie_afterInsert(item, context) {
setTimeout(() => {
$w(‘#textSaveForm’).hide(‘FadeOut’);
}, 1500);
}

export function SaveData_click() {
let toInsert = {
“dataEvento”: $w(" #ImptData “).value,
“tipoEvento”: $w(” #ImptEvent “).value,
“nomeEventoOErimonia”: $w(” #ImputName “).value,
“code”: $w(” #ImptCode “).value,
“utenteRedirect”: $w(” #ImpuUser “).value,
“redirect”: $w(” #ImputUrl “).value
};
wixData.insert(“RedirectCerimonie”, toInsert)
.then( (results) => {
$w(” #textSaveForm “).text = “Form salvato con successo”;
$w(” #textSaveForm ").show();
hideImput()
})
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
});
}

I can not figure out what the difference is, so I hook up the hooks.
In my opinion they must be very useful to shorten the code but I do not understand how and when to use them.

Thank you

Hi,

Data hooks are typically used to manipulate data before or after saving to the collection. For example, changing to fields to all CAPs or, or calculating a sum, etc. I would recommend reading the article About Data Hooks .

You can also get some “hands-on” experience by playing with the tutorial How to Use Data Hooks . Trying it out yourself in the editor will help you understand the what and how of data hooks.

Have fun,

Yisrael

Thanks for the answer, I already tried to look at the guide, but I could not understand.
I looked in the forum for some real examples of situations in which hooks are used because with the examples it is easier to test but I could not find them.

I can not understand how these are recalled. In the example I did at the beginning I tried to change the command

console.log (‘Try AfterInsert’ + context);

but it gives me the error undefine context.

So I do not know how to use them. It’s not that you can help me with a causal example

I’m not sure, but in your case context might in fact be undefined . That’s not a problem as long as you don’t try to use it. As I mentioned earlier, context isn’t always needed. And in some cases it won’t even be set - it will be undefined .

You can also look at the examples in wix-data.Hooks to see different use cases.

The best way to understand is to read the different docs, and then to play with them yourself in the editor. Then you’ll see what works and what doesn’t, and what all the different types of hooks do.

Good luck,

Yisrael

Forgive me, but why does not this work?

export function RedirectCerimonie_afterInsert() {

console.log('try AfterInsert'); 

}

export function SaveData_click() {
let toInsert = {
“dataEvento”: $w(" #ImptData “).value,
“tipoEvento”: $w(” #ImptEvent “).value,
“nomeEventoOErimonia”: $w(” #ImputName “).value,
“code”: $w(” #ImptCode “).value,
“utenteRedirect”: $w(” #ImpuUser “).value,
“redirect”: $w(” #ImputUrl “).value
};
wixData.insert(“RedirectCerimonie”, toInsert)
.then( (results) => {
$w(” #textSaveForm “).text = “Form salvato con successo”;
$w(” #textSaveForm ").show();
})
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
});
}

Thank You

Hi Yisrael

I open this post to tell you that I understood where I was wrong.

I did not understand where hooks should be written, I wrote it on the page instead of writing it in data.js

Actually it was enough to read more guides and do other tests.

Thank you!!!

1 Like

Wow! Glad that worked out for you. Thanks for letting me know.