#Server - Using `send` causes a 404 error but using `sendError` works as expected, why?

5 messages · Page 1 of 1 (latest)

plucky oar
#

In the most simple example:

In my index.vue I have:

const { data, error } = await useFetch('/tester')
console.log('data', data.value)
console.log('error', error.value)

And my /server/routes/tester.ts:

export default defineEventHandler((event) => {
  send(event, { test: 'ok' })
})

This causes a 404 error to occur: error Error: 404 Cannot find any route matching /tester. (/tester)

However returning data directly does work, and using sendError works as expected. Any idea as to why this is? Thanks!

plucky oar
#

I still haven't figured this out but have since moved to returning data as such:

event.node.res.statusCode = 400
event.node.res.statusMessage = error?.message || 'An error occurred.'
return {}
#

or a 200 response with no data:

event.node.res.statusCode = 200
return {}
violet wing
#

if you don't return it is 404, use return createError(...)

plucky oar
#

ah I see, ty! so I presume the sendError() from unjs h3 is calling a return, hence why that works