Help, does the middleware example in the Deno Fresh documentation not work for anyone else?
I tried to follow the example in Deno Fresh about middlewares, but it doesn't work, the content
ctx.state.data
doesn't appears to be undefined whatever I do2 Replies
I'm sorry to hear that you're having trouble with the middleware example in the Fresh documentation. Let's try to debug this issue together. 🦕
First, let's make sure that you're correctly setting the
ctx.state.data
in your middleware. Here's the example from the documentation:
In this example, ctx.state.data
is not being set. If you want to set ctx.state.data
, you would need to do so in one of your middleware functions. For example:
And then include setData
in your handler
array:
If you've done this and ctx.state.data
is still undefined, there might be an issue with the order of your middleware functions. The ctx.next()
function will call the next middleware in the handler
array, so if you're trying to access ctx.state.data
in a middleware function that comes before setData
in the handler
array, ctx.state.data
will be undefined.
If none of these suggestions solve your issue, could you please provide more details aboutyour code and the specific error message you're seeing? That will help me assist you better.