繁体   English   中英

如何使用nodejs和mongoose获取多个JSON对象?

[英]How to get multiple JSON objects using nodejs and mongoose?

我想获取所有具有season:2008season:2009的JSON对象。 对于季节字段,有2008、2009、2010、2011、2012、2013、2014、2015、2016年值。

我想通过API端点获取所有具有field season:2008 JSON对象。 请参阅下面的代码以获取更多说明,但它仅返回null

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

码:

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

});

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

每当我访问URL http://localhost:5000/api/matches/2008http://localhost:5000/api/matches/2010它都为null

在/ api / matches /:season之前的http:// localhost:5000 / api / matches / 2010之前,应该有一个路由名称。 假设您的路由名称是match.js,那么您的网址应类似于http:// localhost:5000 / match / api / matches / 2010 取决于您如何定义index.js

现在您的两条路由是相同的...您必须为两个查询创建不同的路由...因为查询不知道什么是match_id和session ...如果您为这些路由保留相同的名称,则可以查询之前的代码将首先执行

改变你的两条路线

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

    app.get(/api/matches/:season)

暂无
暂无

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

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