简体   繁体   中英

How to POST dropdown form data to MongoDB using express/nodejs?

Sorry if this is a dumb question, I'm super new to express and mongodb/mongoose so not sure what I'm doing wrong. I'm trying to have a few dropdown menus where the user can make selections from each dropdown and then click submit to send a POST request to my database. I got it working with a form in which you type your own data but I only want the user to be able to select from a dropdown...

here's the dropdown form i'm trying to create the POST request from:

<form action="/environments" method="POST"></form>
    <select>
        <% environments.forEach(function(environment){ %>
        <option value="name"><%= environment.name %></option>
        <% }); %>
    </select>
    <select>
        <% environments.forEach(function(environment){ %>
        <option value="region"><%= environment.region %></option>
        <% }); %>
    </select>
    <input type="submit" />
</form>

here's my app.js

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

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

//DISPLAY ALL ENVIRONMENTS IN DB
app.get("/environments", function(req, res) {
  //get all environments from db
  Environment.find({}, function(err, allEnvironments) {
    if (err) {
      console.log(err);
    } else {
      res.render("environments", { environments: allEnvironments });
    }
  });
});

//POST FORM DATA TO DB
app.post("/environments", function(req, res) {
  //get data from form and add to db
  var name = req.body.name;
  var region = req.body.region;
  var newEnvironment = { name: name, region: region };
  //create new env and save to db
  Environment.create(newEnvironment, function(err, newlyCreated) {
    if (err) {
      console.log(err);
    } else {
      //redirect back to environments
      res.redirect("/environments");
    }
  });
});

You have to set name for each select tag. For your case it will be name and region , because this a values you want to post back to server.

Then, in each option tag of each select tag, you have to set value for them, if you set <option value="name"><%= environment.name %></option> , this mean you always get back value is name for every choices.

Finally, the ejs code (I think so) will be like:

<form action="/environments" method="POST"></form>
    <select name="name">
        <% environments.forEach(function(environment){ %>
        <option value="<%= environment.name %>"><%= environment.name %></option>
        <% }); %>
    </select>
    <select name="region">
        <% environments.forEach(function(environment){ %>
        <option value="<%= environment.region %>"><%= environment.region %></option>
        <% }); %>
    </select>
    <input type="submit" />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM