Can't get if/else function to work for results field

Hi,
I’m trying to design a sports prediction site where the result of the game is marked down as ‘home (win)’ or ‘away (win)’ depending on the scoreline. I can get ‘home’ to appear in the result field when I use the beforeInsert data hook but not with the afterGet hook (does it matter which hook I use here?). But I can’t get it to alternate between ‘home’ and ‘away’ when I change the scorelines.
Is ‘let hookContext = context’ a default line or do you have to change the hookContext part to a relevant field in your code, eg. ‘let game1_result = context’? Both of these options give me a ‘unread’ warning so I’m all confused but I also think this is where the problem is, although I can’t seem to fix it at the moment.
Here’s the code I’ve done up. Any help would be greatly appreciated as my forehead is currently red raw from banging it repeatedly against the table.

export function Week1_Predictions2_beforeInsert(item, context) {
//TODO: write your code here…
let hookContext = context;
var game1_h = (‘game1_h’).valueOf();
var game1_a = (‘game1_a’).valueOf();
var game1_result = (‘game1_result’).toString();

if (game1_h > game1_a) {
item.game1_result = ‘home’;
}
else {
item.game1_result = ‘away’;
}
return item;
}
console.log(‘game1_result’);

Why are you using data hooks? The record affected is inside item but I don’t understand what you are doing with the data hook. Can you please try to show a page and the result you would expect.

Hi Andrea. Thanks for looking at this for me. Here’s a link to the page in question but it’s not fully deisnged yet as I’m focussing on the back-end code first

I’ve read a lot of Q&As on this forum and data hooks are normally suggested - either beforeInsert or afterGet so that’s why I was using them. Basically I want the user to enter in predictions and then the results field wiill have the word ‘home’ or ‘away’ in it’s field depending on the game’s scoreline.

Hi,
I’m not sure what you were trying to achieve here:

var game1_h = ('game1_h').valueOf(); 
var game1_a = ('game1_a').valueOf();

This is incorrect Javascript statement. Can you clarify what you were trying to declare here?

Thanks,
Tal.

Hi Tal,

I’m trying to declare that the variables game1_h and game1_a are the numerical values that appear in the ‘game1_h’ and ‘game1_a’ data boxes within the predictions database. I’m basically trying to write a function that puts the word ‘home’ in the Results text field if the scoreline is , say, 3-2; and puts the word ‘away’ in the Results text field if the scoreline is, say 2-3.

ThanksPaddy

Hi. Any ideas on how I can get started with this as I feel like I’m going round in circles. Should I forget about hooks and use a wix query function, or similar?

Thanks
Paddy