gpt4 book ai didi

javascript - 如何将数组从 Textarea 发送到 mongoDB?

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:57 25 4
gpt4 key购买 nike

我正在尝试发布一个序列号数组,但是一旦发布,文本区域中添加的所有序列号都会作为单个字符串发布在一起。

这是我的表格:

<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 上的样子

enter image description here

我正在尝试创建序列号输入,以便我们可以跟踪保修时间。下面是我希望如何存储数据的示例。

{
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);

关于javascript - 如何将数组从 Textarea 发送到 mongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51794917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com