[英]How can I push multiple values from inputs in a form to an array of objects in mongoose.model?
I am making a list of bibliography entries using a form adding values to a mongoose.model but certain entries would require to have multiple authors.我正在使用向 mongoose.model 添加值的表单制作参考书目条目列表,但某些条目需要有多个作者。
To add an author I have added a js script assigned to a button to add inputs for the author's first name and second name but when I submit the form instead of creating multiple author objects in an array of objects defined in a schema it just pushes all the first name values and second name values into the same object.为了添加作者,我添加了一个分配给按钮的 js 脚本,以添加作者的名字和第二名的输入,但是当我提交表单而不是在模式中定义的对象数组中创建多个作者对象时,它只会推送所有内容将第一个名称值和第二个名称值合并到同一个对象中。 How could I differentiate different authors while using the same inputs to add multiple author objects to the array of authors?
在使用相同的输入将多个作者对象添加到作者数组时,如何区分不同的作者?
This is the Schema:这是架构:
const BiblibliographySchema = new mongoose.Schema({
author: [{
firstName: "String",
secondName: "String"
}],
title: "String",
publishInfo: {
publisher: "String",
location: "String"
},
date: "Number"
});
module.exports = mongoose.model("Bibliography", BiblibliographySchema);
This is the form:这是表格:
<form class="bibliography-form" action="/workspace/bibliography" method="POST">
<div id="add-input">
<input type="text" name="bibliography[author][firstName]"
placeholder="First Name">
<input type="text" name="bibliography[author][secondName]"
placeholder="Second Name">
<button id="add-name" type="button">+</button>
<br>
</div>
</form>
This is the js script that adds input fields:这是添加输入字段的js脚本:
var button = document.getElementById("add-name");
button.addEventListener("click", function(){
var br = document.createElement('br');
document.getElementById("add-input").appendChild(br);
var input1 = document.createElement('input');
var input2 = document.createElement('input');
input1.setAttribute('type', 'text');
input1.setAttribute('name', 'bibliography[author][firstName]');
input1.setAttribute('placeholder', 'First Name');
input2.setAttribute('type', 'text');
input2.setAttribute('name', 'bibliography[author][secondName]');
input2.setAttribute('placeholder', 'Second Name');
document.getElementById("add-input").appendChild(input1);
document.getElementById("add-input").appendChild(input2);
document.getElementById("add-input").appendChild(br);
});
And this is the create route:这是创建路线:
app.post('/workspace/bibliography', function(req, res){
Bibliography.create(req.body.bibliography, function(err, newEntry){
if (err) {
console.log(err);
} else {
res.redirect('/workspace/bibliography');
}
});
});
This his how it is the bibliography entry is displayed on the page:这是他的参考书目条目显示在页面上的方式:
<div>
<% bibliography.forEach(function(entry){ %>
<form action="/workspace/bibliography/<%=entry._id%>?
_method=DELETE" method="POST">
<p>
<%= entry.author.secondName %>, <%= entry.author.firstName %>.
<i><%= entry.title %>. <%= entry.publishInfo.publisher %></i>,
<%= entry.publishInfo.location%>, <%= entry.date %>.
<button class="delete-bibliography-entry">x</button>
</p>
</form>
<% }); %>
</div>
I would like the output to look like this:我希望输出如下所示:
but right now it looks like this:但现在它看起来像这样:
thank you for any help!!!感谢您的任何帮助!!!
Try to replace this line尝试替换此行
<%= entry.author.secondName %>, <%= entry.author.firstName %>
. <%= entry.author.secondName %>, <%= entry.author.firstName %>
。 with this有了这个
<% for(var i=0; i<entry.author.secondName.length; i++) {%>
<%= entry.author.secondName[i] %>, <%= entry.author.firstName[i] %> /
<% } %>
Updated : The way you post your data is the problem.更新:您发布数据的方式是问题所在。 It handles all your inputs as a single string, that's why it stores all
firstName
and secondName
in one array element and not to many.它将所有输入作为单个字符串处理,这就是为什么它将所有
firstName
和secondName
存储在一个数组元素中而不是多个元素中。 The proper way to post an array of objects is to make an AJAX request.发布对象数组的正确方法是发出 AJAX 请求。 Just for your case now, if you want to find a quick solution, you could use only one input (eg
fullName
) and display the whole name.就您现在的情况而言,如果您想快速找到解决方案,您可以只使用一个输入(例如
fullName
)并显示全名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.