Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route params dont seem to work for PUT or DELETE #864

Open
acidjazz opened this issue Aug 13, 2024 · 3 comments
Open

route params dont seem to work for PUT or DELETE #864

acidjazz opened this issue Aug 13, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@acidjazz
Copy link

acidjazz commented Aug 13, 2024

Environment

in /server/api/[...slug].ts i have

const router = createRouter()
router.put('/user/:user/pen/:pen', update)

then my handler function has the following.

const update = defineEventHandler(async (event) => {
  console.log(event.context.params)
....

which ends up showing:

{ _: 'user/1/pen/1' }

Reproduction

ill whip up a stackblitz asap and report back

Describe the bug

i should see context.params.id and context.params.user populated

Additional context

No response

Logs

No response

@acidjazz acidjazz added the bug Something isn't working label Aug 13, 2024
@acidjazz acidjazz changed the title route params dont seem to work for PUT route params dont seem to work for PUT or DELETE Aug 16, 2024
@acidjazz
Copy link
Author

confirmed for DELETE as well

@pi0
Copy link
Member

pi0 commented Aug 16, 2024

Thanks, please ping me when made reproduction but i would guess issue is from old radix3 behavior. If you can try against h3-nightly (v2) that would be amazing.

@acidjazz
Copy link
Author

acidjazz commented Aug 17, 2024

https://stackblitz.com/edit/github-zdmzwb?file=server%2Fapi%2F%5B...slug%5D.ts

I found that this was because I did not prepend the route with a /

import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();

router.get(
  '/**',
  defineEventHandler((event) => 'not found')
);

router.put(
  'user/:user/pen/:id',
  defineEventHandler((event) => {
    return {
      parms: event.context.params,
    };
  })
);

export default useBase('/api/', router.handler);

returns :

data : {
  "parms": {
    "_": "user/1/pen/2"
  }
} 

while adding the slash:

import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();

router.get(
  '/**',
  defineEventHandler((event) => 'not found')
);

router.put(
  '/user/:user/pen/:id',
  defineEventHandler((event) => {
    return {
      parms: event.context.params,
    };
  })
);

export default useBase('/api/', router.handler);

works fine:

data : {
  "parms": {
    "user": "1",
    "id": "2"
  }
} 

i'm not sure if this is expected behavior, if it is im happy to close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants