0

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.

12
  • Looks like your response is empty. What do you get when you query the db directly on such aquery? Commented Jan 21, 2018 at 12:27
  • @Zlatko No that query works on db check imgur.com/a/xkFzf Commented Jan 21, 2018 at 12:31
  • @Zlatko check out the question I have edited it Commented Jan 21, 2018 at 12:33
  • can you set mongoose.set('debug', true); in js and see the query fired? Commented Jan 21, 2018 at 13:04
  • @Saravana In app.js or matches.js Commented Jan 21, 2018 at 13:11

2 Answers 2

0

Now your both the routes are identical...you have to make different routes for both queries... because queries does not know what is match_id and session... if you keep same name for the routes then query which is before in the code will be executed first

change your both the routes

    app.get('/api/match_id/:match_id)

    app.get(/api/matches/:season)
Sign up to request clarification or add additional context in comments.

Comments

0

Before http://localhost:5000/api/matches/2010 before the /api/matches/:season there should be a route name. Lets say your route name is match.js you the url would look like http://localhost:5000/match/api/matches/2010. Depends how you defined the index.js

6 Comments

I have created matches.js when I go to http://localhost:5000/api/matches it shows this JSON objects (total 577 JSON objects) imgur.com/a/ae60C
I just want to get thos JSON objects how have season:2008 fields when I navigate to this URL http://localhost:5000/api/matches/2008 it gives null
What changes do I need to make in app.get()
Example in app.Get: var match = require('./routes/matches.js'); app.use('/matches', match); Try something like this. This must be in index.js
I think I my app.get() is wrong any idea how to fix it ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.