'What is this error "Cast to ObjectId failed for value "shop.css" (type string) at path "_id" for model "Product""?

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?



Solution 1:[1]

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.

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eol
Solution 2 Pawan