2016-04-19 16 views
0

私は、ユーザーがリソースを保存し、それらに関する記事を作成するWebサイトを持っています。現時点では、ユーザーが記事でコメントを付けることができるコメントセクションを作成しています。私は記事のIDとコメントを送るコントローラからトリガされるput要求を持っています。PUT要求解析エラー

が、私は次のエラーが表示され、ここで

SyntaxError: Unexpected token 1 
    at parse (/home/themis/webappionio/node_modules/body-parser/lib/types/json.js:83:15) 
    at /home/themis/webappionio/node_modules/body-parser/lib/read.js:116:18 
    at invokeCallback (/home/themis/webappionio/node_modules/raw-body/index.js:262:16) 
    at done (/home/themis/webappionio/node_modules/raw-body/index.js:251:7) 
    at IncomingMessage.onEnd (/home/themis/webappionio/node_modules/raw-body/index.js:308:7) 
    at emitNone (events.js:67:13) 
    at IncomingMessage.emit (events.js:166:7) 
    at endReadableNT (_stream_readable.js:905:12) 
    at doNTCallback2 (node.js:441:9) 
    at process._tickCallback (node.js:355:17) 

は私server.jsです:

//defining Articles Model 

var ArticleSchema = mongoose.Schema({ 
    _creatorname: String, 
    title : String, 
    body : String, 
    resource: String, 
    published : String, 
    comments: [{ 
     _commentorname: String, 
      content : String, 
      date : String 
    }] 
}); 

var Articles = mongoose.model("Articles", ArticleSchema); 

//pushing comment into the specific article 

app.put("/home/:id", function(req,res){ 
    var _commentorname = req.user.username; 
    var content = req.body.comment; 
    var date = moment().tz("Europe/Athens").format("DD/MM/YY HH:mm"); 
    Articles.findByIdAndUpdate(
     id, 
     {$push: {"comments" : {_commentorname: _commentorname, content: content, date: date}}}, 
     {safe: true, upsert: true, new : true}, 
     function(err, article) { 
       console.log(err); 
     } 
    ); 
}); 

マイコントローラ:

$scope.addComment = function(id, comment) { 
     console.log(id); 
     console.log(comment); 
     $http.put("/home/" + id, comment) 
     .success(function(response){ 
      $scope.all(); 
     }); 
    }; 

と私のHTMLフォーム:

<div class="panel-footer"> 
         <input type="text" id="userComment" ng-model="comment" class="form-control input-sm chat-input" placeholder="Write your message here..." /> 
         <span class="input-group-btn">  
          <button class="btn btn-primary btn-sm" ng-click="addComment(article._id, comment)"><span class="glyphicon glyphicon-comment"></span> Add Comment</button> 
         </span> 

        </div> 

は、ここに私の最後のserver.jsある

app.put("/home/:id", function(req,res){ 
    var id = req.params.id; 
    Articles.findByIdAndUpdate(
     id, 
     {$push: {"comments" : {_commentorname: req.user.username, content:req.body.comment}}}, 
     {safe: true, upsert: true, new : true}, 
     function (err, results) { 
      if (err) 
      { 
       res.send("there was a problem updating the information: " + err); 
      } 
      else { 
       res.format({ 
        json: function() { 
         res.json(results); 
        } 
       }); 
      } 
     } 
    ); 
}); 
+1

あなたがスタックを解釈することを学んだ場合、それが役立つだろうが少し良くトレースします。エラーソースは "body parser"から来ています。つまり、フォーマットされたJSONに構造エラーがあります。また、「プロジェクト」がさまざまなテクノロジーコンポーネントをスタックに使用しているからといって、あなたの「問題」または「質問」に「すべて」が含まれているわけではありません。一般的に、これらのものを絞り込み、適切に質問してタグ付けすることが期待されます。 –

答えて

1

問題はここcommentが実際にObjectとしてではなく、単に「文字列」として送信されていないことです。だから、代わりにObjectを構成する必要があります。

ので、正確な角度コントローラのコードは:

$scope.addComment = function(id, comment) { 
     console.log(id); 
     console.log(comment); 
     $http.put("/home/" + id, { "comment": comment}) 
     .success(function(response){ 
      $scope.all(); 
     }); 
    }; 
+0

@ExWeiまた、間違った推測をして、間違っていると言われました。 –

+0

うまくいきました。私は上記のようにオブジェクトを押しました。私は日付を押​​してみようとすると、何もプッシュしていないという日付フォーマット以外のものを私はすべてプッシュしました。 –

+1

@ニックボーそれは実際問題ではない理由は事実です。あなたのスキーマタイプは現在、 '' date "'プロパティの "文字列"です。したがって、単に文字列化する必要があります。私は個人的には、代わりに 'Date'として格納する必要があり、文字列をローカルのタイムゾーンに強制しないようにする必要があると思います。しかし、それは遭遇した質問やエラーについてではありません。 [あなたの回答を受け入れる](http://stackoverflow.com/help/accepted-answer)も覚えておいてください。私はあなたにまだ気づいていないので、何も受け入れていません。 –

-1

新しい回答:実際に

それあなたがアンギュラコントローラに「生」のようにコメント変数を提出する代わりに送信されるようになります。それはJSONとして、あなたは

$scope.addComment = function(id, comment) { 
     console.log(id); 
     console.log(comment); 
     $http.put("/home/" + id, {comment: comment}) 
     .success(function(response){ 
      $scope.all(); 
     }); 
    }; 

旧回答にそれを変更する必要があります。

私はそれが動作するネイティブJSオブジェクトを使用する場合、異常な形式 "DD/MM/YY HH:mm"に日付をフォーマットするので、エラーがスローされると思います。

var date = moment().tz("Europe/Athens").toDate()