【问题标题】:How to post an Array from Textarea to mongoDB?如何将数组从 Textarea 发布到 mongoDB?
【发布时间】:2019-01-18 13:34:45
【问题描述】:

我正在尝试发布一个序列号数组,但是一旦发布,在 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 上的样子

enter image description here

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

{
productId: "laptops",
serial: [
11111,
22222, 
44444,
 ]

}

【问题讨论】:

  • 请提供有关所需输出的信息。此外,您不必附加图像......只需输入对象输出以及您希望数据看起来像什么。
  • 你好@Akrion 我刚刚编辑了帖子,我添加了我希望如何将我的参数存储在数据库中,我想在文本区域中传递几组数字,然后将它们存储为连载中的数组。我希望这会有所帮助。非常感谢

标签: javascript jquery node.js express post


【解决方案1】:

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

这样修改数据怎么样?

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

【讨论】:

  • Ray 非常感谢,这样就解决了这个问题,只是为了好奇,你建议如何在文本区域分隔几组数字?用逗号,输入还是空格?例如: ex1: 4444,5555,6666 exp2: 44444 555555 66666 exp3: 4444 55555 66666 谢谢
  • 欢迎 :) 如果您在向用户显示数据时谈论视觉方面,“输入”分隔值在文本区域中看起来最整洁。假设您有不超过 6 到 7 个组。否则使用逗号。附言你也可以赞成我的回答吗?赞成票有助于获得徽章。也谢谢你:)
  • 嘿,Ray 谢谢你的提示,我试图投票,但它没有让我,它说记录了那些声望低于 15 的人的投票,但不要更改公开显示的分数:( ,但我做到了。你介意先提出这个问题,这样我的声誉就会提高吗?谢谢
  • 啊,我明白了,不用担心。给你!
猜你喜欢
  • 1970-01-01
  • 2020-07-19
  • 2017-11-29
  • 1970-01-01
  • 2020-06-14
  • 2018-11-27
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多