首页 > 解决方案 > 如何将数组从 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,
 ]

}

标签: javascriptjquerynode.jsexpresspost

解决方案


这可能是因为您使用的是用于字符串数据的文本区域。

像这样修改数据怎么样?

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


推荐阅读