1

I'm new to mongoose/mongo and express so I'm not quiet sure how to go about this. So basically I'm trying to use checkboxes to sort of "filter" results in my database.

Here's an example of something in my db:

{ "location" : "PHARMACY",
"region" : "SOCAL",
"system" : "WITS",
"contact" : "SMITH",
"membership" : "TIER1" }

So far I've only figured out how to hard code it like this, where I do Environment.find({region: "SOCAL"}) and it spits out all the db entries that contain SOCAL as a region:

var express = require("express"),
  app = express(),
  mongoose = require("mongoose"),
  bodyParser = require("body-parser");

mongoose.connect("mongodb://localhost/epims", {
  useNewUrlParser: true,
  useUnifiedTopology: true
});
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");

//schema setup
var environmentSchema = new mongoose.Schema({
  name: String,
  region: String,
  system: String,
  contact: String,
  membership: String,
});

var Environment = mongoose.model("Environment", environmentSchema);

app.get("/environments/show", function(req, res) {
  //find environment with selected parameters
  Environment.find({region: "SOCAL"}, function(err, foundEnvironment) {
    if (err) {
      console.log(err);
    } else {
      //render show template with that env
      res.render("show", { environment: foundEnvironment });
    }
  });
});

How do I make it so this search through the database changes based on what selections are made?

Here is my select form if anybody needs to see that as well

<form action="/environments" method="GET">
    <legend>Locations</legend>
    <input type="checkbox" name="location" value="PHARMACY">
    <label for="PHARMACY">PHARMACY</label>

    <input type="checkbox" name="location" value="OFFICE">
    <label for="OFFICE">OFFICE</label>

    <input type="checkbox" name="location" value="STORE">
    <label for="STORE">STORE</label>

    <legend>Regions</legend>
    <input type="checkbox" name="region" value="SOCAL">
    <label for="SOCAL">SOCAL</label>

    <input type="checkbox" name="region" value="NORCAL">
    <label for="NORCAL">NORCAL</label>

    <input type="checkbox" name="region" value="WA">
    <label for="WA">WA</label>

    ...
<a href="/environments/show">Submit</a>
</form>
4
  • Do you need help with API code ? So from this form how does inputs flow to API endpoint ? As I see it's a GET endpoint does these inputs flow as query params ? If yes then you can take those values & form a query for DB transaction.. Commented Feb 25, 2020 at 20:42
  • Can you post your whole server code ? Are you parsing any body (e.g. json middleware for express) For get request you should use the search query in the url. in a get request no body/data should be send, because a get request should not be manipluated based on the body data. You can use POST or any other method when you want to use a body payload Commented Feb 25, 2020 at 21:02
  • @Marc hey yes sorry about that, I use bodyparser and just added the server code above! Is a POST request used even when I'm just trying to retrieve info from the database? Commented Feb 25, 2020 at 21:47
  • 1
    @Emily, you can use what ever you want. Get is perfectly fine when you just want to fetch some data. If you want to transfer some data fromnthe client to server, use PUT or POST. Take a short look of the REST specifiaction gor better understanding Commented Feb 26, 2020 at 0:58

1 Answer 1

2

You can pass the search parameters via the search query: /environments/show?location=OFFICE&location=STORE&region=SOCAL&region=NORCAL

The GET request produce this database query:

{ 
  region: { '$in': [ 'SOCAL', 'NORCAL' ] },
  location: { '$in': [ 'OFFICE', 'STORE' ] } 
}

app.get("/environments/show", function (req, res) {

     let region = [req.query.region || []].flat();
let location = [req.query.location || []].flat();


let query = {
    region: {
        "$in": region
    },
    location: {
        "$in": location
    }
};



    Environment.find(query, function (err, foundEnvironment) {
        if (err) {
            console.log(err);
        } else {
            //render show template with that env
            res.render("show", { environment: foundEnvironment });
        }
    });

});
<form action="/environments/show" method="GET">
    <legend>Locations</legend>
    <input type="checkbox" name="location" value="PHARMACY">
    <label for="PHARMACY">PHARMACY</label>

    <input type="checkbox" name="location" value="OFFICE">
    <label for="OFFICE">OFFICE</label>

    <input type="checkbox" name="location" value="STORE">
    <label for="STORE">STORE</label>

    <legend>Regions</legend>
    <input type="checkbox" name="region" value="SOCAL">
    <label for="SOCAL">SOCAL</label>

    <input type="checkbox" name="region" value="NORCAL">
    <label for="NORCAL">NORCAL</label>

    <input type="checkbox" name="region" value="WA">
    <label for="WA">WA</label>

    ...
    <button type="submit">Submit</button>
</form>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.