简体   繁体   中英

How to get specific JSON data from REST API using Nodejs?

I want to get JSON data of a particular lets say match=2 data from all matches. I am not able to get required data which is stored in MongoDB.

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

I am able to get all JSON data of 577 total matches using below code.

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

    matches.find({}).then(eachOne => {

        res.json(eachOne);

    });

});

But now I do not want to get JSON data of all matches instead I want to get JSON data of match having id:2 how can I get that I tried using below code but it gets JSON data of all matches.

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

    let match = req.params.id;

    matches.find({match}).then(eachOne =>{

        res.json(match);
    });

}); 

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;

Your code is almost right. You just need to specify the right query:

app.get('/api/matches/:match_id', (req, res) =>{
    let match = req.params.match_id;
    matches.findOne({id: parseInt(match)}).then(m =>{
        res.json(m);
    });
});

Also consider that match_id from url is a string, so you need to check it and convert to number (I used parseInt )

You must be using bodyparser by default .

You are doing wrong.

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



    // instead of req.params.id write req.params.match_id
   let match = req.params.match_id;

    // when you write {match} there is nothing {id : match}

    matches.find({match_id : match}).then(eachOne =>{

        res.json(eachOne);
    });

}); 

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