简体   繁体   English

如何使用 express/nodejs 将下拉表单数据发布到 MongoDB?

[英]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.对不起,如果这是一个愚蠢的问题,我是表达和 mongodb/mongoose 的超级新手,所以不确定我做错了什么。 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.我正在尝试使用一些下拉菜单,用户可以在其中从每个下拉菜单中进行选择,然后单击提交将 POST 请求发送到我的数据库。 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:这是我尝试从以下位置创建 POST 请求的下拉表单:

<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这是我的 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.对于您的情况,它将是nameregion ,因为这是您想要回发到服务器的值。

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.然后,在每个option标签的每个select标签中,你必须为它们设置值,如果你设置了<option value="name"><%= environment.name %></option> ,这意味着你总是能得到值是每个选择的name

Finally, the ejs code (I think so) will be like:最后,ejs 代码(我认为是这样)将类似于:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 nodejs / express:如何用数据过帐到外部表单? - nodejs / express: How to POST to an external form with data? 如何从nodejs中的多步表单发布数据并表达到MongoDB? - How to post data from a Multi-Step Form in nodejs and express to MongoDB? 如何在使用 NodeJS、Express 和 mongodb 将其更新为新数据之前检索旧表单数据! ejs是模板吗? - How to retrieve older form data before updating it to new one using NodeJS, Express and mongodb! Ejs is the template? 使用Express和Mongoose将表单数据发布到现有MongoDB文档数组中 - Post Form Data to Array of Existing MongoDB Document using Express and Mongoose 如何使用nodejs / express将数据存储在mongodb中? - How can I store data in mongodb using nodejs / express? 使用带有Express的NodeJS从MongoDB中检索数据 - Retrieving data from MongoDB using NodeJS with Express 使用nodeJS(Express)从表单将关系数据(objectId)发布到mongoDB时面临问题 - Facing issue while posting relational data(objectId) to mongoDB from a Form using nodeJS(Express) 使用AJAX在nodeJS Express MongoDB中发送post请求 - Using AJAX to send post request in nodeJS Express MongoDB 如何使用MULTER,Nodejs和MySQL发布表单数据(图像和数据) - How to post form data ( image and data ) using MULTER, Nodejs and MySQL 如何使用nodejs和mongodb将单个表单的数据插入两个collections - How to insert data of a single form into two collections using nodejs and mongodb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM