简体   繁体   中英

Error while fetching documents from a collection in MongoDB

I am developing an App where I have to filter by sportsname. I am using Node.js and MongoDB in the Backend.

Here is my Code:-

router.route('/user/:_id/explore/:sportsname')

.get(function(req, res){


var query = Host.find();

    if(req.params.sportsname){
            query.where({sportsname:decodeURIComponent(req.params.sportsname)});
    }

    query.exec(function(err, explore){
            if(err)
                    return next(err);
            res.json(sportsname);
            console.log(sportsname);
    })

Here is the document. I want to search the users by sportsname

[  
   {  
      "_id":"5460e2af4ee5216f17000061",
      "maxplayer":"1",
      "minplayer":"11",
      "date":"10 August 2014",
      "venue":"google",
      "time":"Afternoon",
      "sportsname":"Lawn Tennis",
      "__v":0
   },
   {  
      "_id":"5460ff1b4ee5216f17000065",
      "maxplayer":"0",
      "minplayer":"9",
      "date":"10 October 2014",
      "venue":"google",
      "time":"Early Morning",
      "sportsname":"Cricket",
      "__v":0
   },
   {  
      "_id":"5461a4014ee5216f17000089",
      "maxplayer":"4",
      "minplayer":"29",
      "date":"13 December 2014",
      "venue":"google",
      "time":"Afternoon",
      "sportsname":"Lawn Tennis",
      "__v":0
   }
]

When I am running on that time it is giving an error. The error is given below:-

TypeError: path must be a string at Query.where 
(/node/node-api-master/node_modules/mongoose/lib/query.js:593:11) 
at Object.handle (/node/node-api-master/server.js:202:19) at next_layer 
(/node/node-api-master/node_modules/express/lib/router/route.js:103:13) 
at Route.dispatch (/node/node-api-master/node_modules/express/lib/router/route.js:107:5) 
at /node/node-api-master/node_modules/express/lib/router/index.js:195:24 
at param (/node/node-api-master/node_modules/express/lib/router/index.js:268:14) 
at param (/node/node-api-master/node_modules/express/lib/router/index.js:280:16) 
at param (/node/node-api-master/node_modules/express/lib/router/index.js:280:16) 
at Function.proto.process_params 
(/node/node-api-master/node_modules/express/lib/router/index.js:296:3) 
at next (/node/node-api-master/node_modules/express/lib/router/index.js:189:19)

This is a syntax problem, here is a right implementation of what you're trying to do :

router.get('/user/:_id/explore/:sportsname', function(req, res, next) {
  return Host.find({ sportsname: req.params.sportsname }, function(err, users) {
    if (err) return next(err);
    console.log(users);
    return res.json(users);
  });
});

Note that checking if req.params.sportsname exists is not necessary, since your route will never be reached if it's not. If you want to make this parameter optional, use the following : /user/:_id/explore/:sportsname? (adding a question mark after your parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM