I am new to all this and am not a coder. I need help writing a hook so that the items entered into my collections are not duplicated but rather updated or if new entered as such. All items should be connected to the Unit # column. This is the first part of 3 I need to solve. I would need someone to write the code for me or explain it so that I can understand it.
Hi, please share some more context about what you are trying to do.
hooks are used, for example, to change the data just before you update it or just after reading it to enrich it. for example convert the item cost to a different currency, or format it in another way.
what it seems you want to do is make a decision before the data is sent to a collection to be saved
Shlomi
Thanks for responding Shlomi, What I have is a members only form that is filled in by the member. It will track the location of product within a company. What I want is for my input form to acknowledge the unit # as either a new number or a existing number. If new it will enter the information and the new number. If it exists then the rest of the information would be updated retaining the original #. Units are entered into the form as items, columns hold the relevant information for the unit. Each unit has a number such as B145 or A654. Its always a letter followed by a number. I was told hooks would help, but if there is another way that would be great. Any suggestions would be greatly appreciated.
Hi,
Hook can help, but I suggest a different approach using ‘wixData.query()’, ‘wixData.insert()’, ‘wixData.update()’ .
You’ll also need to make a customised submit button using an onClick() function.
it will look a bit like that:
export function button1_click(event) {
const unitNumber = $w('textInput1').value;
const note = $w('textInput2').value;
const location = $w('#dropdown1').value;
wixData.query('collectionName').eq("unitNumber", unitNumber).find().then(result => {
if (result.items.length === 1) {
//item is in data base, update it:
const updatedItem = result.items[0];
// do some updating
wixData.update('collectionName', updatedItem);
} else {
wixData.insert('collectionName', updatedItem);
}
})
}
Please note that this code need to be adjust to your site.
Liran.
Thank You Liran,
I tried to insert and adjust the code you gave me, but I came up with some errors. Can you tell me what I did wrong?
Line #1 Says: ESLint failed to validate this file because an error occurred: 0 is undefined
Line 6 says: Multiple annotations:
Unexpected token
Undetermined string constant
Thanks again,
Dan
Try removing the ’ mark before your wixData.query call.
I removed it, new set of errors.
7 - Parameter “event” never used.
9- “note” is unread
10- “Location” is unread
11,16,18- “wixData” is undefined
18- “updated item” is undefined.
Hi Dan… it’s just an example. please try adjusting the code it to your site.
I’ve tried to add more info.
Please read comments (starting with ‘//’) to understand each step.
Comments are not being executed, just notation used to help you understand the code
export function button1_click(event) {
const unitNumber = $w('textInput1').value;
const note = $w('textInput2').value;
const location = $w('#dropdown1').value;
wixData.query('collectionName').eq("unitNumber", unitNumber).find().then(result => {
if (result.items.length === 1) {
//item is in data base, update it:
const updatedItem = result.items[0];
// do some updating
wixData.update('collectionName', updatedItem);
} else {
//create a new item and put on data base
const newItem = {
unitNumber,
note,
location
}
wixData.insert('collectionName', newItem);
}
})
}
Liran.