Confirm value in database and then update

Hi.

I am seeking to create a database with preuploaded values where only specific registered users will be allowed to create new values for the database. On an input page, I am then trying to have general users input information into one value field which will firstly confirm that the value is already in the pre-uploaded database. All the pre-uploaded information will be set to a specific value beforehand in a second value field. When the user inputs the information into the first value field and presses a button, automatically I am trying to have the second value field updated (the options in this second value field are dichotomous). I am creating two separate pages registered to different users so that each user is only allowed to make a specific change in the second value field. Ie. user 1 can only change from the dichotomous values in one direction for eg (a-b and not b-a). Then I would create a second page which does the same for user 2 but only allows this user to change the second value field in the reverse direction (ie. from b-a and not a-b).To do this final restriction i believe i can just copy the code to the other page and reverse the update aspect of the code.

I’ve been reading other forums and adapting codes provided as guides and this is as far as I have been able to get. Assistance would be greatly appreciated.

import wixData from ‘wix-data’;
//all log messages will show when we run this code.
//they will help us debug and see all is working well.
//they have no impact other than that and can be safely removed
//once we see the code is working well
console.log(“in before insert hook”);
console.log("number is " + item.ticketNumber);

//create a new promise object.  
//in the promise's body we'll look for dups. 
//if we find any, we will reject the promise and abort the insert. 
let p = new Promise(function(resolve, reject) 
{ 
	console.log("in promise body"); 
	
	//create a query to look for items with the same number as the item we're tring to insert 
	let number_q = wixData.query("Tickets").eq("ticketNumber", item.ticketNumber); 
	let admit_q = wixData.query("Tickets").eq("Admittance", item.admittance) 

		
	//run the query to confirm that the item is registered 
	number_q.count().then(count => 
	{ 
		console.log("after big_q returns"); 
		if (count === 1) 
		{ 
			console.log("Ticket found, resolving the promise to the original 'item' so the insert can proceed");			 
			resolve(item); 
		} 
		else 
		{ 
			console.log("count is zero, rejecting the promise with an error message"); 
			reject("Ticket not found!"); 
		} 
	}); 
	
}); 

//return the promise from the hook 
return p; 

}

Hey Kareem,

Sounds good! It seems like you have everything planned out, and need to implement Data Hooks when data is inserted to your collections. If you haven’t yet, you should check out this article about how Data Hooks work with WixData, and this article about how to use them.

Let me know how that works for you,
J.

Thank Jonathan, I’ve never done any coding before so I’m pretty much going by trial and error, so unfortunately I’m now stuck somewhere in the process of having the database update. I’ve been able to identify the value is in the database and I’ve also been able to have the update executed to the specific column i require. However, only the first item in the database is being updated and not the item which the user searched for via the query. I looked into and thought an _afterquery hook might be useful after the comment however I’m not sure if i’m including it correctly because from hereon the script doesn’t run.

Basically all i need now is to use the item which was utilized in the query as the item to update and not to arbitrarily update the first item in the database.

Much appreciate any assistance!