javascript - 如何将数组从 Textarea 发布到 mongoDB?
问题描述
我正在尝试发布一个序列号数组,但是一旦发布,在 textarea 中添加的所有序列号都会作为一个字符串一起发布。
这是我的表格:
<form class="" id="serialsForm" action="/serialsnew" method="post">
<h5 style="text-align:center">Procesar RMA</h5>
<input placeholder="Product ID"class="input1" type="text"
name="productId" value="">
<textarea id="textAreaSerial" class="newSeriales" placeholder="#
Seriales"name="serial[]" rows="3" cols="80" ng-trim="false"> .
</textarea>
<button type="submit" value="send" class="btn btn-primary
buttonNewRma" data-toggle="modal" id="submit">
submit
</button>
</form>
这是我的发帖请求
app.post("/serialsnew", function(req, res){
var productId = req.body.productId;
var serialNum = req.body.serial;
Seriales.create({
product: productId,
seriales: serialNum,
}, function(err, serial){
if(err){
console.log("we coulndt add the serial numbers")
} else{
console.log("we just added a patch of serials")
}
})
res.json(req.body);
// res.redirect("/serialsnew")
})
这就是我的数组在 res.json 上的样子
我正在尝试创建序列号输入,以便我们可以跟踪保修时间。下面是我想如何存储数据的示例。
{
productId: "laptops",
serial: [
11111,
22222,
44444,
]
}
解决方案
这可能是因为您使用的是用于字符串数据的文本区域。
像这样修改数据怎么样?
//this is basically your req.body, with sample serial values
var req = {body: {serial:["2423,23423"]}};
var stringGroup = req.body.serial[0];
var serial = stringGroup.split(",").map(Number);
console.log(serial);
推荐阅读
- glsl - 在 KDevelop 中关闭 GLSL 的语义分析
- c# - 在.Net Windows Forms中循环时不阻塞UI
- javascript - 如何将 + 号移到左侧?
- python - 使用 cbv 隐藏模板中的内容
- excel - 按范围复制和粘贴值,而不是按单元格
- django - 我正在通过 api 调用从外部数据库同步应用程序详细信息,出现错误 none type has no attribute strip
- sql - 如何使用 SQL 中的“IN”运算符编写 LDAP 查询
- r - 汇总数据,转换为时间序列并用零补全缺失的月份
- javascript - 如何更改 Yup 的日期格式验证
- javascript - 如何在 vue.js 中将数据传递给 tinymce?