为什么展开运算符在 for 循环中只显示最后输入的数据?
Why spread operator is showing only the last entered data in a for loop?
在 GET 请求中,我在 for 循环中使用展开运算符,但它仅显示数据库中最后输入的数据(关于座位的数据)。
GET 请求:
router.get("/trip/single", async (req, res) => {
if (!req.query.from || !req.query.to || !req.query.date) {
return res.send({
error: "Please enter the data to get the trip",
});
}
const { from, to, date } = req.query;
const routes = await Route.find({
"Location.from": from,
"Location.to": to,
date: date.toString(),
});
const matchedBus = await routes.filter(() => {
return Route.busId === routes._id;
});
const bookings = await Booking.find({
routeId: { $in: matchedBus.map((matchedBus) => matchedBus._id) },
});
const busIdWithSeatsObj = {};
for (let i = 0; i < matchedBus.length; i++) {
let currentBusSeats = [];
const busBookings = routes.map((route) => {
return route.date === date && route.busId === matchedBus[i].busId;
});
bookings.forEach((booking) => {
currentBusSeats = [...booking.seatNumbers];
});
busIdWithSeatsObj[matchedBus[i].busId] = currentBusSeats;
}
res.status(200).send({ routes, matchedBus, busIdWithSeatsObj });
});
我有两个collections进入数据库:
{"_id":{"$oid":"629a0efd4202a387c33186f9"},
"userId":{"$oid":"629a0e044202a387c33186e7"},
"routeId":{"$oid":"629a0ea54202a387c33186f5"},
"passengers": [{"name": "mitul","gender": "male","age": 25, "_id": {"$oid": "629a0efd4202a387c33186fa"}}],
"phone": 7984154363,
"email":"mitulkheni695@gmail.com",
"bookingDate":"2022-06-01",
"fare": 2500,
"seatNumbers": [1, 2, 3, 4, 5],
"departureDetails": [{"city": "surat","location": "kapodra","time": "8:00:00", "date": "2022-06-03","_id": {"$oid": "629a0efd4202a387c33186fb"}}],
"arrivalDetails": [{"city":"bhavnagar", "location": "paravadi", "time": "12:00:00", "date": "2022-06-03","_id": {"$oid":"629a0efd4202a387c33186fc"}}],
"createdAt":{"$date":"2022-06-03T13:39:09.223Z"},
"updatedAt":{"$date": "2022-06-03T13:39:09.223Z"},
"__v": 0}
{"_id": {"$oid": "629ac54b13b8d8cd52fc743d"},
"userId":{"$oid":"629a0e044202a387c33186e7"},
"routeId":{"$oid":"629a0ea54202a387c33186f5"},
"passengers":[{"name": "dhaval", "gender": "male", "age": 24, "_id": { "$oid": "629ac54b13b8d8cd52fc743e" }}],
"phone": 7984154363,
"email":"dhaval@gmail.com",
"bookingDate": "2022-06-04",
"fare": 1000,
"seatNumbers": [14, 15],
"departureDetails":[{"city": "surat", "location": "laskana", "time":"8:00:00", "date": "2022-06-03", "_id": { "$oid": "629ac54b13b8d8cd52fc743f" }}],
"arrivalDetails": [{"city": "bhavnagar", "location": "sukhpar", "time": "12:00:00", "date": "2022-06-03", "_id": { "$oid": "629ac54b13b8d8cd52fc7440" }}],
"createdAt": {"$date": "2022-06-04T02:36:59.215Z"},
"updatedAt": {"$date": "2022-06-04T02:36:59.215Z"},
"__v": 0}
在这个 collections 中都有相同的 routeId。
我应该得到这个结果,所有的座位都已经为这辆特定的公共汽车预订了:
{
"routes": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"matchedBus": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"busIdWithSeatsObj": {
"629a0e184202a387c33186ec": [
1,
2,
3,
4,
5,
14,
15
]
}
}
但我得到的是 这个 结果,仅包含最后输入的数据:
{
"routes": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"matchedBus": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"busIdWithSeatsObj": {
"629a0e184202a387c33186ec": [
14,
15
]
}
}
在 for 循环中,您将用新数组覆盖 currentBusSeats 数组,而不是附加到它。您创建一个新数组并在此处覆盖旧的 currentBusSeats:
currentBusSeats = [...booking.seatNumbers];
要添加到 booking.seatNumbers,您可以使用 Array.push:
currentBusSeats.push(...booking.seatNumbers);
旁注:现在您还可以使用 let 将 currentBusSeats 声明为 const 而不是变量,这将通过获取试图覆盖数组的错误(赋值给常量变量)来帮助您进行调试。我认为始终将对象和数组声明为常量是一种很好的做法,因为您几乎不想覆盖它们,只需更改值即可。
在 GET 请求中,我在 for 循环中使用展开运算符,但它仅显示数据库中最后输入的数据(关于座位的数据)。
GET 请求:
router.get("/trip/single", async (req, res) => {
if (!req.query.from || !req.query.to || !req.query.date) {
return res.send({
error: "Please enter the data to get the trip",
});
}
const { from, to, date } = req.query;
const routes = await Route.find({
"Location.from": from,
"Location.to": to,
date: date.toString(),
});
const matchedBus = await routes.filter(() => {
return Route.busId === routes._id;
});
const bookings = await Booking.find({
routeId: { $in: matchedBus.map((matchedBus) => matchedBus._id) },
});
const busIdWithSeatsObj = {};
for (let i = 0; i < matchedBus.length; i++) {
let currentBusSeats = [];
const busBookings = routes.map((route) => {
return route.date === date && route.busId === matchedBus[i].busId;
});
bookings.forEach((booking) => {
currentBusSeats = [...booking.seatNumbers];
});
busIdWithSeatsObj[matchedBus[i].busId] = currentBusSeats;
}
res.status(200).send({ routes, matchedBus, busIdWithSeatsObj });
});
我有两个collections进入数据库:
{"_id":{"$oid":"629a0efd4202a387c33186f9"},
"userId":{"$oid":"629a0e044202a387c33186e7"},
"routeId":{"$oid":"629a0ea54202a387c33186f5"},
"passengers": [{"name": "mitul","gender": "male","age": 25, "_id": {"$oid": "629a0efd4202a387c33186fa"}}],
"phone": 7984154363,
"email":"mitulkheni695@gmail.com",
"bookingDate":"2022-06-01",
"fare": 2500,
"seatNumbers": [1, 2, 3, 4, 5],
"departureDetails": [{"city": "surat","location": "kapodra","time": "8:00:00", "date": "2022-06-03","_id": {"$oid": "629a0efd4202a387c33186fb"}}],
"arrivalDetails": [{"city":"bhavnagar", "location": "paravadi", "time": "12:00:00", "date": "2022-06-03","_id": {"$oid":"629a0efd4202a387c33186fc"}}],
"createdAt":{"$date":"2022-06-03T13:39:09.223Z"},
"updatedAt":{"$date": "2022-06-03T13:39:09.223Z"},
"__v": 0}
{"_id": {"$oid": "629ac54b13b8d8cd52fc743d"},
"userId":{"$oid":"629a0e044202a387c33186e7"},
"routeId":{"$oid":"629a0ea54202a387c33186f5"},
"passengers":[{"name": "dhaval", "gender": "male", "age": 24, "_id": { "$oid": "629ac54b13b8d8cd52fc743e" }}],
"phone": 7984154363,
"email":"dhaval@gmail.com",
"bookingDate": "2022-06-04",
"fare": 1000,
"seatNumbers": [14, 15],
"departureDetails":[{"city": "surat", "location": "laskana", "time":"8:00:00", "date": "2022-06-03", "_id": { "$oid": "629ac54b13b8d8cd52fc743f" }}],
"arrivalDetails": [{"city": "bhavnagar", "location": "sukhpar", "time": "12:00:00", "date": "2022-06-03", "_id": { "$oid": "629ac54b13b8d8cd52fc7440" }}],
"createdAt": {"$date": "2022-06-04T02:36:59.215Z"},
"updatedAt": {"$date": "2022-06-04T02:36:59.215Z"},
"__v": 0}
在这个 collections 中都有相同的 routeId。
我应该得到这个结果,所有的座位都已经为这辆特定的公共汽车预订了:
{
"routes": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"matchedBus": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"busIdWithSeatsObj": {
"629a0e184202a387c33186ec": [
1,
2,
3,
4,
5,
14,
15
]
}
}
但我得到的是 这个 结果,仅包含最后输入的数据:
{
"routes": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"matchedBus": [
{
"Location": {
"from": "629a0e544202a387c33186f2",
"to": "629a0e294202a387c33186ef"
},
"_id": "629a0ea54202a387c33186f5",
"busId": "629a0e184202a387c33186ec",
"date": "2022-06-03",
"departureTime": 8,
"arrivalTime": 12,
"createdAt": "2022-06-03T13:37:41.993Z",
"updatedAt": "2022-06-03T13:37:41.993Z",
"__v": 0
}
],
"busIdWithSeatsObj": {
"629a0e184202a387c33186ec": [
14,
15
]
}
}
在 for 循环中,您将用新数组覆盖 currentBusSeats 数组,而不是附加到它。您创建一个新数组并在此处覆盖旧的 currentBusSeats:
currentBusSeats = [...booking.seatNumbers];
要添加到 booking.seatNumbers,您可以使用 Array.push:
currentBusSeats.push(...booking.seatNumbers);
旁注:现在您还可以使用 let 将 currentBusSeats 声明为 const 而不是变量,这将通过获取试图覆盖数组的错误(赋值给常量变量)来帮助您进行调试。我认为始终将对象和数组声明为常量是一种很好的做法,因为您几乎不想覆盖它们,只需更改值即可。