Home > Software engineering >  how to retrieve all matched data from database in node.js from query string?
how to retrieve all matched data from database in node.js from query string?

Time:05-24

I am building a bus ticket booking app in node.js. I have created 4 tables. 1 - users table, 2 - bus table, 3 - booking table, 4 - route table.

here's the trip model:

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,
  });

In that trip model only administrator(authenticated user) can add data about trip(like departure-Location, arrival-Location, bus-data and date)

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();
 }
});

suppose there are search boxes for user to enter the details of the trip like this one

https://i.stack.imgur.com/oXvsj.png

User enters the data and that data converted into query string (like this: 127.0.0.1:3000/trips?departure=surat&arrival=bhavnagar&date=2022-05-30) and based on that query string I want to show the all matched trips to the user.

now I want to filter the data according to user's(non-authenticated users as well) need but I don't know how to do that.

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

I have embedded the seat data in the bus model

const busSchema = new mongoose.Schema(
  {
    busNumber: {
      type: String,
      unique: true,
      required: true,
    },
    seats: {
      type: Number,
      required: true
    },
  },
  {
    timestamps: true,
  }
);

how to show the users the bus and seats available for that matched trips

CodePudding user response:

You can filter the data using the find function:

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(router);
});
  • Related