如何使用Node.js和Mongoose获取多个JSON对象? [英] How to get multiple JSON objects using nodejs and mongoose?

查看:217
本文介绍了如何使用Node.js和Mongoose获取多个JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想通过API端点获取所有具有字段season:2008的JSON对象.有关更多说明,请参见下面的代码,但仅返回null.

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响应:

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

});

在matches.js中:

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;

每当我转到URL http://localhost:5000/api/matches/2008http://localhost:5000/api/matches/2010时,它都会给出null.

Whenever I go to URL http://localhost:5000/api/matches/2008 or http://localhost:5000/api/matches/2010 it gives null.

推荐答案

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

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

更改两条路线

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

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

这篇关于如何使用Node.js和Mongoose获取多个JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆