我根据从源文件夹中读取的文件在ejs中构建表单。每个文件都会在表单中生成许多字段。用户可以向每个文件添加元数据,在提交整个表单时,数据应保存在mongodb中。那么我如何在字段中定义名称,以便理想情况下我可以将整个req.body直接传递给mongodb,就像这个问题:Post Multiple JSON Objects Simultaneously with Express and Postman。 我尝试过使用参数化名称,例如name =" size_<%= data [i] .invNr%>但是我必须做很多解析才能回到单个对象。
<form action = "/" method = "POST">
<div class="form-group">
<% for (var i = 0; i < length; i++) { %>
<div class="row">
<div class="col-md-4"><a href="<%= data[i].fullPath %>"><img src="<%= data[i].fullPath %>" width="200" class="media-object"></a></div>
<div class="col-md-8"><i class="fa fa-certificate"></i>
<span>Original: <%= data[i].origFile %></span>
<br>
<span name="katnr">KatalogNr: <%= data[i].katNr %></span>
<br>
<span name="invnr">InventarNr: <%= data[i].invNr %></span>
<br>
<span>FullP: <%= data[i].fullPath %></span>
<br>
<span>Test: <%= data[i].test %></span>
<br>
<input type = "hidden" id="invnr_<%= data[i].invNr %>" name ="invnr" value="<%= data[i].invNr %>">
<input type = "hidden" id="fullpath_<%= data[i].invNr %>" name ="fullpath" value="<%= data[i].fullPath %>">
<span>Comment: <%= data[i].comment %></span>
<br>
<span>Color</span>
<input type = "text" id="color_<%= data[i].invNr %>" name ="color" value="Viggo<%= data[i].invNr %>">
<br>
<span> Size</span>
<input type = "text" id="size_<%= data[i].invNr %>" name = "size" value="Otto <%= data[i].invNr %>" >
</div>
</div>
<hr>
<% } %>
<input type = "submit" value = "Submit">
</div>
</form>
谢谢!
答案 0 :(得分:0)
<强> HTML 强>
<form action = "/" method = "POST">
<div class="form-group">
<% for (var i = 0; i < length; i++) { %>
<div class="row">
<div class="col-md-4"><a href="<%= data[i].fullPath %>"><img src="<%= data[i].fullPath %>" width="200" class="media-object"></a></div>
<div class="col-md-8"><i class="fa fa-certificate"></i>
<span>Original: <%= data[i].origFile %></span>
<br>
<span name="katnr">KatalogNr: <%= data[i].katNr %></span>
<br>
<span name="invnr">InventarNr: <%= data[i].invNr %></span>
<br>
<span>FullP: <%= data[i].fullPath %></span>
<br>
<span>Test: <%= data[i].test %></span>
<br>
<input type = "hidden" id="invnr_<%= data[i].invNr %>" name ="invnr[]" value="<%= data[i].invNr %>">
<input type = "hidden" id="fullpath_<%= data[i].invNr %>" name ="fullpath[]" value="<%= data[i].fullPath %>">
<span>Comment: <%= data[i].comment %></span>
<br>
<span>Color</span>
<input type = "text" id="color_<%= data[i].invNr %>" name ="color[]" value="Viggo<%= data[i].invNr %>">
<br>
<span> Size</span>
<input type = "text" id="size_<%= data[i].invNr %>" name = "size[]" value="Otto <%= data[i].invNr %>" >
</div>
</div>
<hr>
<% } %>
<input type = "submit" value = "Submit">
</div>
<强>控制器强>
var main = {}
for(var i = 0;i<req.body.invnr.length;i++){
var obj ={
invnr:req.body.invnr[i],
fullpath:req.body.fullpath[i],
fullpath:req.body.color[i],
fullpath:req.body.size[i]
}
main.push(obj);
}
console.log(JSON.stringify(main))