PUT请求解析错误

时间:2016-04-19 09:55:24

标签: javascript angularjs node.js

我有一个网站,用户可以在其中存储资源并制作有关它们的文章。目前,我正在构建评论部分,用户可以在文章中发表评论。我有一个put请求,它是从控制器触发的,它发送文章的id和注释。

但是我收到以下错误,

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

2 个答案:

答案 0 :(得分:1)

问题在于comment这里实际上并不是作为Object发送,而是作为&#34;字符串&#34;。因此,您需要构建Object

因此纠正角度控制器代码:

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

答案 1 :(得分:-1)

新答案:

实际上,您似乎是在Angular控制器中提交了一个&#34; raw&#34;而不是将其作为JSON发送,您需要将其更改为

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

旧答案:

我认为它会抛出错误,因为您将日期格式化为不常见的格式&#34; DD / MM / YY HH:mm&#34;,如果您使用本机JS对象,它将起作用,例如。

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