0

I am making get request to API endpoint using NodeJS and mongodb. In app.js:

app.get('/api/matches', (req, res) =>{

    //console.log('Get matches..');
    matches.find({}).then(eachOne => {

        res.json(eachOne);

    });

});

Above code gives JSON response see below:

enter image description here

Now I want to get JSON response of a particular match using app.get() method but whenever I use below code it always returns JSON data of id:1 for different URL's like http://localhost:5000/api/matches/3 or http://localhost:5000/api/matches/6

app.get('/api/matches/:match_id', (req, res) =>{

    let match = req.params.id;

    matches.findOne({match_id: match}).then(m =>{

        res.json(m);
    });

});

Above two URL'S give same JSON data but they should give different JSON data.

{
  "_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": ""
}

In matches.js <---Model

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;
9
  • check whether you are passing user_defined or mongoDb generated in the GET request. Commented Jan 21, 2018 at 9:25
  • I have edited my question please see matches.js file Commented Jan 21, 2018 at 9:27
  • do you have many documents with the same id? Commented Jan 21, 2018 at 9:30
  • @Saravana No id is unique id=1,2,3,4,5,...577 Commented Jan 21, 2018 at 9:31
  • 1
    @Saravana I have answered my question please check it out. Commented Jan 21, 2018 at 9:38

4 Answers 4

1

I have finally fixed the problem here is the code: In schema field name for id was match_id. Field in schema match_id is same as id in document. You also need to use parseInt(match) as URL is string and needs to be converted in to type:Number

app.get('/api/matches/:match_id', (req, res) =>{
    let match = req.params.match_id;
    matches.findOne({id: parseInt(match)}).then(m =>{
        res.json(m);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

@t.niese Should I add some explanation ?
@t.niese I have added explanation please check if you like it please upvote :)
0

you can try this... gives only two fields city and team1

app.get('/api/matches', (req, res) =>{

        //console.log('Get matches..');
        matches.find({},{ city: 1, team1: 1 }).then(eachOne => {

            res.json(eachOne);

        });

    });

Comments

0

I think you have issue when query with 'match_id' instead of '_id', i don't see it in your collection.

Comments

0

Your findOne should be

matches.findOne({ id : match }).then(m =>{ ... } //match id to be matched

Comments

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.