簡體   English   中英

Node.js讀取傳入的數據

[英]Node.js read incoming data

我開始學習node.js。 但是我在讀取傳入數據時遇到問題。 title,content,postId在閱讀時沒有任何麻煩。 但我看不懂“類別[]”

// 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>\r\n',
      'categories[]':
       [ '5a7310e7cfa4c52440957bde',
         '5a7310eccfa4c52440957bdf',
         '5a7310f0cfa4c52440957be0',
         '5a7310f5cfa4c52440957be1' ],
      postId: '5a7628be95d1d63668b9da31' }
    new selected categories:  undefined
    PUT /auth/new-post 200 209.076 ms - 2

盡管出現了新的選定類別,但似乎尚未定義。 我怎么能滿足。

先感謝您。

您的類別'categories[]': json字段是錯誤的。

使用以下json字符串應能正常工作

{ 
    "title": "post title",
    "content": "<p>post content</p>\r\n",
     "categories":
       [ "5a7310e7cfa4c52440957bde",
         "5a7310eccfa4c52440957bdf",
         "5a7310f0cfa4c52440957be0",
         "5a7310f5cfa4c52440957be1"],
      "postId": "5a7628be95d1d63668b9da31" 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM