Hook works sometimes

I am having an issue figuring out why the following hook only works sometimes.

export function stallSales_afterInsert(item, context) {
	let trainerBlock;
	const createNewBlock = {
		"trainerCellPhone": item.trainerCellPhone,
		"title": item.trainerName,
		"horseShowId": item.horseShowId,
		"stallsPaidFor": item.type1StallsPurchased + item.type3StallsPurchased + item.type2StallsPurchased,
	}
	wixData.query('StallBlocks')
		.eq('trainerCellPhone', item.trainerCellPhone)
		.eq('horseShowId', item.horseShowId)
		.find()
		.then((results) => {
			console.log(results)
			if (results.items < 1) {
				console.log("need to add block")
				wixData.insert('StallBlocks', createNewBlock)
				return item;
			} else {
				trainerBlock = results.items[0];
				trainerBlock.stallsPaidFor = item.type3StallsPurchased + item.type2StallsPurchased + item.type1StallsPurchased + trainerBlock.stallsPaidFor;
				wixData.save("StallBlocks", trainerBlock)
				return item;
			}
		})
}

Here is where I am getting lost, it creates a new block every time, but it doesn’t always return the total paid for part :

"stallsPaidFor": item.type1StallsPurchased + item.type3StallsPurchased + item.type2StallsPurchased,

My fields are all set to numbers, but nothing seems to work. Also, it does not do the else portion of the code, it hasn’t done that once. Any ideas or obvious issues?

I didn’t read your full post but at quick glance I can see you’re missing a “return” before the query.
It should be:

 //....
return wixData.query('StallBlocks')
		.eq('trainerCellPhone', item.trainerCellPhone)
//...

Also I think you’re missing a “return” before the save() and insert() and then() after the save() and insert().

Thank you for the fast response. Is this what your saying it should look like?

		.then((results) => {
			console.log(results)
			if (results.items < 1) {
				console.log("need to add block")
				return item,	
				wixData.insert('StallBlocks', createNewBlock);
			} else {

or do I need a () after the insert? That is the part I am confused on. What I am doing is querying another dataset, and then based off that query inserting into the other dataset or updating the 1st (and in theory only) item in the other dataset.

@doughammack no. That’s not what I meant.
what about:

return wixData.insert('StallBlocks', createNewBlock).then(() => {return item;});

?

But I really shouldn’t tel give you advice without reading it carefully. So take it with a grain of salt.

@jonatandor35 I have the first part of the hook working great, but the second half is not, any ideas?

export function stallSales_afterInsert(item, context) {
	let trainerBlock;
	let stallcount = item.type1StallsPurchased + item.type3StallsPurchased + item.type2StallsPurchased;

	const createNewBlock = {
		"trainerCellPhone": item.trainerCellPhone,
		"title": item.trainerName,
		"horseShowId": item.horseShowId,
		"stallsPaidFor": stallcount,
	}
	wixData.query('StallBlocks')
		.eq('trainerCellPhone', item.trainerCellPhone)
		.eq('horseShowId', item.horseShowId)
		.find()
		.then((results) => {
			console.log(results)
			if (results.items.length === 0) {
				console.log("need to add block")
				wixData.insert('StallBlocks', createNewBlock);
				return item;
			} else {
				let qresults = results.items[0];
				let oldcount = qresults.stallsPaidFor;
				qresults.stallsPaidFor = (Number(oldcount) + Number(stallcount));
				wixData.update("StallBlocks", qresults);
				return item;
			}
		})
}

When it executes the query, if there is already something in there and the query returns a result that is not 0 it leaves stallsPaidFor as blank. Any ideas?