0

I know this type of question already asked here . I have also looked at following question . But it didn't help , those question deals with when parameter is the mongoose object id . But in my case batch_id is not mongoose object id .

What's Mongoose error Cast to ObjectId failed for value XXX at path “_id”?

Express/Mongoose Router: 'Cast to ObjectId failed for value “undefined” at path “_id”'

Here is what i did .

router.route("/view_by_batch/?:batchid").get(function(req,res){


    if(req.session.user){
        barcodeEntry.find({batch_id:req.params.batchid},function(err, data) {
            if (err) return console.error(err);
            res.render('view_by_batch',{ data: data,batch_no:req.params.batchid});
        });
    }else {
        res.render('index',{auth:false});
    }

});

I am getting the following error in console

message: 'Cast to number failed for value "style.css" at path "batch_id"',
  name: 'CastError',
  kind: 'number',
  value: 'style.css',
  path: 'batch_id' }

Even I removed the link of style.css file but , it didn't help .

Community
  • 1
  • 1
aroSuv
  • 127
  • 3
  • 14

3 Answers3

2

When your page is loading, along with, it loads the styles.css file which is wrongly being routed.

So when the :batchid is being sent, it first sends the desired id first and then immediately "styles.css" file is also gets loaded and it is pointing to a wrong path.

So actually your routing is incorrect.

This is what you should do:

  1. Include following line in your app.js file.

    app.use(express.static( __dirname + "/public"));
    
  2. In your styles.css link tag,

    <link rel="stylesheet" href="/stylesheets/style.css"/>
    
  3. In my case, styles.css file is saved in public/stylesheets/style.css, Please follow instructions according to your own folder structure.

  4. Notice the '/' at the start of href in link tag.

Chinmay Atrawalkar
  • 942
  • 1
  • 8
  • 6
0

This issue is that in your schema you set batch_id as number and you are passing style.css (a string) in the query. Mongoose is trying to do something like parseFloat('style.css') but it fails. So please make sure {batch_id:req.params.batchid} in

barcodeEntry.find({batch_id:req.params.batchid},function(err, data)

is something meaningful.

According to Express Doc1. ? in the path will be treated as a regular expression character. If you just want to match the path param. It's better to set it like

router.route("/view_by_batch/:batchid")

You can see Express Doc for more info about req.params in Express

David
  • 241
  • 2
  • 11
  • Hi David. It's best to ask questions using the comment feature rather than as an answer to the question. It's likely that non-answers will get removed. If you think this is the answer to the question, perhaps rephrase it? – stef Jan 31 '16 at 06:11
  • hi @stef. Thanks for you advice. However, I cannot add comment due to insufficient credits. I did update it a bit to make it like an answer even though I am not 100% sure it is the problem. – David Jan 31 '16 at 06:48
0

I have met some problem like this. It is caused by the router order.

You may do like this:

router.route("/some/:id")
    .get(function(){
        // your code
    })

router.route("/some/other")
    .get(function(){
        // your code
    })

Just due to the "/some/:id" route is before "/some/other" route. When you send a GET request to "/some/other", express considers "other" as a parameter of "/some/:id" route. So you will meet the CastError.

Solution:

Let the route with "id" at the bottom of any other with the same prefix route, the problem will be solved.

router.route("/some/other")
    .get(function(){
        // your code
    })

router.route("/some/:id")
    .get(function(){
        // your code
    })

Documentation

Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
Neven.Leung
  • 820
  • 6
  • 12