skip to Main Content

So i am building backend of a blog application and almost every other route is working just fine but for some reason route blogRouter.get(‘/:id’) is not
(This route is suppose to return blog whose id is being send in the get req).
I am building this backend using prisma ,avien(PostgresDB), couldflare workers and Hono

Here is the code i am working with 🙁https://i.sstatic.net/j7fajfFd.png)

I am having problem with this section of code :

`
blogRouter.get(‘/:id’,async (c) => {

const id = c.req.param('id')
const ID =  Number(id)

const prisma = new PrismaClient({
   datasourceUrl : c.env.DATABASE_URL,
}).$extends(withAccelerate())

console.log('Extracted id param:', id)
console.log('Parsed id:', ID)

if(isNaN(ID)){
    return c.json({msg : "InValid id",Number : ID, parms:id})
}

`

And while sending the get request at postman i am getting this result :-
Get request : "http://localhost:8787/api/v1/blog/:2"
Response : { "msg": "InValid id", "Number": null, "parms": ":2" }

2

Answers


  1. http://localhost:8787/api/v1/blog/:2 I think this url is wrong :2 After sending it you convert it with Number(id), but :2 cannot be converted to number

    http://localhost:8787/api/v1/blog/2 try it like this

    Login or Signup to reply.
  2. try this
    blogRouter.get('/:id',async (req,res) => {
    const id = req.params('id')   //or    req.params.id 
    const ID =  Number(id)
    .
    .
    .
    .
     try this
     (remove colon from the URL)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search