如何从查询字符串中检索 node.js 中数据库中的所有匹配数据?
how to retrieve all matched data from database in node.js from query string?
我正在 node.js 构建一个巴士票预订应用程序。我创建了 4 tables。 1 - 用户 table,2 - 公交车 table,3 - 预订 table,4 - 路线 table。
这是行程模型:
const routeSchema = new mongoose.Schema({
departureLocation: {
name: {
type: String,
required: true,
},
subLocation: { type: [String] },
time: {
type: String,
required: true
}
},
arrivalLocation: {
name: {
type: String,
required: true,
},
subLocation: { type: [String] },
time : {
type: String,
required: true
}
},
duration: {
type: Number,
required: true,
},
busId:{
type: mongoose.Schema.Types.ObjectId,
ref:"Bus",
required: true
},
date: {
type:String,
required: true
},
},
{
timestamps: true,
});
在该旅行模型中,只有管理员(经过身份验证的用户)可以添加有关旅行的数据(如出发地点、到达地点、巴士数据和日期)
router.post("/addTrip", auth, async (req, res) => {
const route = new Route(req.body);
try {
await route.save();
res.status(201).send(route);
} catch (e) {
res.status(500).send();
}
});
假设有这样的搜索框供用户输入旅行的详细信息
https://i.stack.imgur.com/oXvsj.png
用户输入数据并将该数据转换为查询字符串(如下所示:127.0.0.1:3000/trips?departure=surat&arrival=bhavnagar&date=2022-05-30)并基于我要显示的查询字符串用户的所有匹配行程。
现在我想根据用户(以及未经身份验证的用户)的需要过滤数据,但我不知道该怎么做。
router.get("/trips", async (req, res) => {
if(!req.query.departure || !req.query.arrival || !req.query.date){
return res.send({
error: "Please enter the data to get the trip"})
}
let departure = req.query.departure;
let arrival = req.query.arrival;
let date = req.query.date;
let routes = await Route.find().lean().exec();
let route = routes.find((route) => {
route.departureLocation.name.toLowerCase() == departure &&
route.arrivalLocation.name.toLowerCase() == arrival &&
route.date == date;
//What to write here
});
})
我已经在巴士模型中嵌入了座位数据
const busSchema = new mongoose.Schema(
{
busNumber: {
type: String,
unique: true,
required: true,
},
seats: {
type: Number,
required: true
},
},
{
timestamps: true,
}
);
如何向用户显示匹配行程的可用巴士和座位
您可以使用 find
函数 filter
数据:
router.get('/trips', async (req, res) => {
if (!req.query.departure || !req.query.arrival || !req.query.date) {
return res.send({
error: 'Please enter the data to get the trip',
});
}
let departure = req.query.departure;
let arrival = req.query.arrival;
let date = req.query.date;
let routes = await Route.find({
departureLocation: departure,
arrivalLocation: arrival,
date
}).lean().exec();
return res.status(200).json(routes);
});
我正在 node.js 构建一个巴士票预订应用程序。我创建了 4 tables。 1 - 用户 table,2 - 公交车 table,3 - 预订 table,4 - 路线 table。
这是行程模型:
const routeSchema = new mongoose.Schema({
departureLocation: {
name: {
type: String,
required: true,
},
subLocation: { type: [String] },
time: {
type: String,
required: true
}
},
arrivalLocation: {
name: {
type: String,
required: true,
},
subLocation: { type: [String] },
time : {
type: String,
required: true
}
},
duration: {
type: Number,
required: true,
},
busId:{
type: mongoose.Schema.Types.ObjectId,
ref:"Bus",
required: true
},
date: {
type:String,
required: true
},
},
{
timestamps: true,
});
在该旅行模型中,只有管理员(经过身份验证的用户)可以添加有关旅行的数据(如出发地点、到达地点、巴士数据和日期)
router.post("/addTrip", auth, async (req, res) => {
const route = new Route(req.body);
try {
await route.save();
res.status(201).send(route);
} catch (e) {
res.status(500).send();
}
});
假设有这样的搜索框供用户输入旅行的详细信息
https://i.stack.imgur.com/oXvsj.png
用户输入数据并将该数据转换为查询字符串(如下所示:127.0.0.1:3000/trips?departure=surat&arrival=bhavnagar&date=2022-05-30)并基于我要显示的查询字符串用户的所有匹配行程。
现在我想根据用户(以及未经身份验证的用户)的需要过滤数据,但我不知道该怎么做。
router.get("/trips", async (req, res) => {
if(!req.query.departure || !req.query.arrival || !req.query.date){
return res.send({
error: "Please enter the data to get the trip"})
}
let departure = req.query.departure;
let arrival = req.query.arrival;
let date = req.query.date;
let routes = await Route.find().lean().exec();
let route = routes.find((route) => {
route.departureLocation.name.toLowerCase() == departure &&
route.arrivalLocation.name.toLowerCase() == arrival &&
route.date == date;
//What to write here
});
})
我已经在巴士模型中嵌入了座位数据
const busSchema = new mongoose.Schema(
{
busNumber: {
type: String,
unique: true,
required: true,
},
seats: {
type: Number,
required: true
},
},
{
timestamps: true,
}
);
如何向用户显示匹配行程的可用巴士和座位
您可以使用 find
函数 filter
数据:
router.get('/trips', async (req, res) => {
if (!req.query.departure || !req.query.arrival || !req.query.date) {
return res.send({
error: 'Please enter the data to get the trip',
});
}
let departure = req.query.departure;
let arrival = req.query.arrival;
let date = req.query.date;
let routes = await Route.find({
departureLocation: departure,
arrivalLocation: arrival,
date
}).lean().exec();
return res.status(200).json(routes);
});