如何使用Node.js和Mongoose获取多个JSON对象?

编程入门 行业动态 更新时间:2024-10-23 19:24:45
本文介绍了如何使用Node.js和Mongoose获取多个JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想获取所有具有season:2008或season: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 localhost:5000/api/matches/2008或localhost:5000/api/matches/2010时,它都会给出null.

Whenever I go to URL localhost:5000/api/matches/2008 or 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对象?

本文发布于:2023-11-22 19:18:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何使用   对象   Node   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!