I want to get all the JSON objects having season:2008 or season:2009. For season field there are 2008,2009,2010,2011,2012,2013,2014,2015,2016 values.
I want to get all JSON objects having field season:2008 through an API endpoint. See below code for more clarification but it only returns null.
JSON response :
{
"_id": "5a63051735aaddd30d1d89cc",
"id": 1,
"season": 2008,
"city": "Bangalore",
"team1": "Kolkata Knight Riders",
"team2": "Royal Challengers Bangalore",
"toss_winner": "Royal Challengers Bangalore",
"toss_decision": "field",
"result": "normal",
"dl_applied": 0,
"winner": "Kolkata Knight Riders",
"win_by_runs": 140,
"win_by_wickets": 0,
"player_of_match": "BB McCullum",
"venue": "M Chinnaswamy Stadium",
"umpire1": "Asad Rauf",
"umpire2": "RE Koertzen",
"umpire3": ""
}
Code:
/* <---Uncomment
app.get('/api/matches/:match_id', (req, res) =>{
let match = req.params.match_id;
matches.findOne({id: parseInt(match)}).then(Match =>{
res.json(Match);
});
});
*/ <---Uncomment
app.get('/api/matches/:season', (req, res) =>{
let Season = req.params.season;
matches.find({season: parseInt(Season)}).then(eachOne =>{
res.json(eachOne);
});
});
In matches.js :
const mongoose = require('mongoose');
let Schema = mongoose.Schema;
const matchSchema = new Schema({
match_id:{
type:Number,
required:true
},
season:{
type:Number,
required:true
},
city:{
type:String,
required:true
},
date:{
type:Number
},
team1:{
type:String,
required:true
},
team2:{
type:String,
required:true
},
toss_winner:{
type:String,
required:true
},
toss_decision:{
type:String,
required:true
},
dl_applied:{
type:Number
},
winner:{
type:String,
required:true
},
win_by_runs:{
type:Number,
required:true
},
win_by_wickets:{
type:Number,
required:true
},
player_of_match:{
type:String,
required:true
},
venue:{
type:String,
required:true
},
umpire1:{
type:String,
required:true
},
umpire2:{
type:String,
required:true
},
umpire3:{
type:String
}
});
const matches = mongoose.model('matches', matchSchema);
module.exports = matches;
Whenever I go to URL http://localhost:5000/api/matches/2008 or http://localhost:5000/api/matches/2010 it gives null.
mongoose.set('debug', true);in js and see the query fired?