Home > Software design >  What is this error "Cast to ObjectId failed for value "shop.css" (type string) at pat
What is this error "Cast to ObjectId failed for value "shop.css" (type string) at pat

Time:12-18

I am trying to extract detail of products entered by that administrator.

Here is the part of code dealing with that:

router.get('/admin-product/:userid', (req, res, next)=>{
    
    Product.find({_id: req.params.userid})    
           .then(prods=>{
                res.render('adminproduct', {
                    prods: prods, 
                    userid: req.params.userid, 
                    name:"vinod"
                });
           })
           .catch(err=>{
               console.log(err);
           })
    
    
})

But when i execute the code i get error as Cast to ObjectId failed for value "shop.css" (type string) at path "_id" for model "Product"

Guide me on what is causing this problem and how to eliminate it?

CodePudding user response:

Looks like your frontend/client is requesting the shop.css styles with the base path /admin-product, i.e. HTTP GET <yourhost>:/admin-product/shop.css.

Since you've declared :usedId as a path parameter the value shop.css will be passed to mongoose through req.params.userid.

Looks to me like you should either fix this on the client-side or fix serving the static content through /admin-product.

CodePudding user response:

I don't know the reason but all I had to do was to change href="./shop.css" to href="/shop.css" in the template link. By doing that the problem was resolved.

Please comment if someone has an explanation for this.

  • Related