afterInsert not seeing item ID (error WDE0002)

I am using an afterInsert hook to send a message after an item is inserted to a collection. The hook works fine when a form is filled out on another local page on the site.

The problem occurs when the hook runs after an item is inserted with an http-function in the backend. The item is inserted into the collection, but the afterInsert hook fails.

Errors received:

"Hook afterInsert for collection MyCollection result ignored! Expected hook result to resolve to an object with an '_id' property, but got [Undefined]"
"afterInsert Error","WDE0002: ItemId must be a string."

When I console.log the item, it has a string id. But if I log the id, it’s undefined:

"item.id: undefined"
"item._id: 08d90f32-fc7f-4d20-a6b7-c8b9292e089f"

Appreciate any help!

It looks like you’re missing return item; in your hook function.

And there’s no such a thing item.id (unless you created such a filed) only item._id

I was trying item.id in reference to this post https://www.wix.com/velo/forum/coding-with-velo/error-itemid-must-be-a-string

It has a return item. The hook function works as expected when ran from the a form that inserts an item on the frontend.

@user234rdfg can you post your code (http-function and hook)?

@jonatandor35 sorry about that, you were right! I was returning the http response in a fetch function, but not the item in the afterInsert . Had to prune down the code to make it shareable and noticed the error. Thank you!