繁体   English   中英

如何使用Node.js从REST API获取特定的JSON数据?

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

我想从所有匹配项中获取特定的JSON数据,比如说match = 2数据。 我无法获取存储在MongoDB中的必需数据。

JSON响应:

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

我可以使用以下代码获得577个匹配项的所有JSON数据。

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

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

        res.json(eachOne);

    });

});

但是现在我不想获取所有匹配项的JSON数据,而是希望获取具有id:2的匹配项的JSON数据。我该如何使用以下代码尝试获取它,但它获取所有匹配项的JSON数据。

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

    let match = req.params.id;

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

        res.json(match);
    });

}); 

在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;

您的代码几乎是正确的。 您只需要指定正确的查询:

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

还要考虑url中的match_id是一个字符串,因此您需要检查它并将其转换为数字(我使用parseInt

默认情况下,您必须使用bodyparser。

你做错了

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);
    });

}); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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