Node.js读取传入的数据


                    

我开始学习node.js。 但是我在读取传入数据时遇到问题。
title,content,postId在阅读时没有任何麻烦。
但我看不懂“ categories []” </ p>

  // NEWPOST.JADE
   $(“#editButton”)。click(function(){
       var formSerialize = $(“ form#newPost”)。serialize();
       $ .ajax({
           类型:“ PUT”,
           网址:“ auth / new-post”,
           数据:formSerialize
       })。done(function(){
           console.log(“ SUCCESS”);
       })。fail(function(){
           console.log(“ FAIL”);
       })
   });

// POST.JS
     router.put('/',upload.single('image'),function(req,res,next){
       console.log(要求正文);
       console.log(“新选择的类别:” + req.body.categories);
       const promise = Post.findByIdAndUpdate(
         req.body.postId,
         需求主体
         {
             新:正确
         }
       );
       promise.then((post)=> {
         res.json(post);
       })。catch((err)=> {
         res.send(500);
       })

     });

//结果CMD
     {title:'post title',
       内容:“

帖子内容</ p>

”,
       '类别[]':
        ['5a7310e7cfa4c52440957bde',
          '5a7310eccfa4c52440957bdf',
          '5a7310f0cfa4c52440957be0',
          '5a7310f5cfa4c52440957be1'],
       postId:'5a7628be95d1d63668b9da31'}
     新选择的类别:未定义
     PUT / auth / new-post 200209.076毫秒-2
</ code> </ pre>

尽管出现了新的选定类别,但似乎仍未定义。 我怎么能满足。</ p>

谢谢。</ p>
     </ div>

展开原文

原文

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个回答


您的类别'categories []':</ code>的json字段错误。</ p>

使用以下json字符串应该可以工作</ p>

  {
     “ title”:“帖子标题”,
     “ content”:“

发布内容</ p>

”,
      “类别”:
        [“ 5a7310e7cfa4c52440957bde”,
          “ 5a7310eccfa4c52440957bdf”,
          “ 5a7310f0cfa4c52440957be0”,
          “ 5a7310f5cfa4c52440957be1”],
       “ postId”:“ 5a7628be95d1d63668b9da31”

}
</ code> </ pre>
     </ div>

展开原文

原文

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" 

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐