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