weixin_33696822 2018-02-04 09:15 采纳率: 0%
浏览 351

Node.js读取传入的数据

I started to learn node.js. But I am having a problem reading the incoming data. title, content, postId does not have any trouble while reading. But I can not read "categories[]"

// NEWPOST.JADE
  $("#editButton").click(function() {
      var formSerialize = $("form#newPost").serialize();
      $.ajax({
          type: 'PUT',
          url: 'auth/new-post',
          data: formSerialize
      }).done(function() {
          console.log("SUCCESS");
      }).fail(function() {
          console.log("FAIL");
      })
  });



// POST.JS
    router.put('/', upload.single('image'), function (req, res, next) {
      console.log(req.body);
      console.log("new selected categories:  " + req.body.categories);
      const promise = Post.findByIdAndUpdate(
        req.body.postId,
        req.body,
        {
            new: true
        }
      );
      promise.then((post) => {
        res.json(post);
      }).catch((err) => {
        res.send(500);
      })

    });


// RESULT CMD
    { title: 'post title',
      content: '<p>post content</p>
',
      'categories[]':
       [ '5a7310e7cfa4c52440957bde',
         '5a7310eccfa4c52440957bdf',
         '5a7310f0cfa4c52440957be0',
         '5a7310f5cfa4c52440957be1' ],
      postId: '5a7628be95d1d63668b9da31' }
    new selected categories:  undefined
    PUT /auth/new-post 200 209.076 ms - 2

Despite the arrival of new selected categories it seemed to undefined. how can i meet it.

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • weixin_33744141 2018-02-04 09:27
    关注

    Your json field of categories 'categories[]': is wrong.

    Use the following json string should work

    { 
        "title": "post title",
        "content": "<p>post content</p>
    ",
         "categories":
           [ "5a7310e7cfa4c52440957bde",
             "5a7310eccfa4c52440957bdf",
             "5a7310f0cfa4c52440957be0",
             "5a7310f5cfa4c52440957be1"],
          "postId": "5a7628be95d1d63668b9da31" 
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题