Hook gives : ["Hook beforeInsert for collection contactForm result ignored! Expected hook result to resolve to an object, but got [Undefined

Hi
Anyone that could possibly point me in the right direction your help will be very much appreciated. I have never posted before so hopefully I don’t break any etiquette or rule it won’t be intentional.

I have a form I created which I wish to alter the date format and add an incremental booking number for each booking made before insert to the collection called contactForm. I can get the hook to work fine when the item is put outside the results query. All works and returns amended ok. (I have to hard code the booking value though as I need to fetch the last number to add on the value to pass it back from the hook from inside the results query of the collection, I cannot share the value outside the IF statement.

However I just get this message:

[“Hook beforeInsert for collection contactForm result ignored! Expected hook result to resolve to an object, but got [Undefined]”//

Obviously, I am being daft, and I am not a good programmer. However, it seems frustratingly correct to me and have tried reading and changing loads of things I keep coming back to I must be close as it works if I hard code the value for the bookingNumber outside the query to the collection.

Thanks for reading and help would be a big help to me.

Coz


// Convert text date from dropdown Wed Jul 28 1993 14:39 selection to database date format Jul 28 1993 14:39 add incremental booking number.//

import wixData from ‘wix-data’

export function contactForm_beforeInsert(item, context) {

//Hook data.js to intercept incall booking and add numerc date format into databese.
//Additionally add in a running uniquie booking nuber incremetally into databse with each booking adding one tolast record//

import wixData from ‘wix-data’ ;

export function contactForm_beforeInsert(item, context) {
// Convert text date from dropdown Wed Jul 28 1993 14:39 selcetion to databse date format Jul 28 1993 14:39

     item.bookingDateTime =  **new**  Date(item.dropdownField); 
     console.log( "indside if statment all works :" ,item.bookingDateTime);   //works as expected // 

    wixData.query( "contactForm" ,item) 
   .gt( 'bookingNumber' , 0 ) 
   .limit( 1 ) 
   .find() 
   .then ((results) => { 

       item.bookingNumber = results.items[ 0 ].bookingNumber ++; 
      console.log( "indside if statement all works : " , item.bookingNumber);    //works as expected // 

return (item); // [“Hook beforeInsert for collection contactForm result ignored! Expected hook result to resolve to an object, but got [Undefined]”//
})
. catch ( (err) => {
let errorMsg = err;
console.log( "Hook error in booking form: " ,err);
}
)

console.log( "item.bookingNumber : ", item.bookingNumber); // show as null as out IF statement //
console.log( “indside if statement all works :” ,item.bookingDateTime); //works as expected //

return (item)
// [“Hook beforeInsert for collection contactForm result ignored! Expected hook result to resolve to an object, but got [Undefined]”//
}

Hi :raised_hand_with_fingers_splayed:

Just return the item as the message asks.
Include this line at the end of the Hook function.

return item;

Ahmad

Thank you Ahmad, for fast reply . However had tried that and same thing happens. More understandibly as the item.bookingNumber value assigned not carried outside the IF statement. When I hard code the value as a test then it will work item.bookingNumber = 10 for example. However I need to assign value increment after the result from collection each time.

console.log( "item.bookingNumber : ", item.bookingNumber); // show as null as out IF statement //
console.log( “indside if statement all works :” ,item.bookingDateTime); //works as expected //

return (item)
// [“Hook beforeInsert for collection contactForm result ignored! Expected hook result to resolve to an object, but got [Undefined]”//
}

@coz You’re also missing a return here:

  return wixData.query("contactForm",item)

I was struggling with this and becoming frustratedly despondent over time. I sincerely thank you for giving up your time and taking the trouble its truly appreciated. I would not have got it working without your help. It now works perfectly after days of me grappling you did in a few minutes, what I could not achieve after 4 days. Once again thank you.

Coz.

1 Like

Yes we’ve all been there. I’m glad it worked for you.