MEAN堆栈:“SyntaxError:意外的标记f”注释不张贴

我正在一个可以回答问题的博客上工作,人们可以评论答案。 询问和回答这个问题很好。 但发表评论不会。 经过调查,我知道其相关的JSON。 可能以某种方式处理身体分析器。 也许我错了。 花了几个小时比较代码,并找不到错误的地方。 这是错误和console.log:

POST http://localhost:8000/answers/5807c9ef24adc7ea591a35b1/comments/ 400 (Bad Request) Object {data: "SyntaxError: Unexpected token f<br> &nbsp; &nbsp;a… &nbsp;at process._tickCallback (node.js:356:17)↵", status: 400, config: Object, statusText: "Bad Request"} config : Object data : "SyntaxError: Unexpected token f<br> &nbsp; &nbsp;at parse (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\lib\types\json.js:83:15)<br> &nbsp; &nbsp;at C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\lib\read.js:116:18<br> &nbsp; &nbsp;at invokeCallback (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\node_modules\raw-body\index.js:262:16)<br> &nbsp; &nbsp;at done (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\node_modules\raw-body\index.js:251:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\node_modules\raw-body\index.js:307:7)<br> &nbsp; &nbsp;at emitNone (events.js:67:13)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:166:7)<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:921:12)<br> &nbsp; &nbsp;at nextTickCallbackWith2Args (node.js:442:9)<br> &nbsp; &nbsp;at process._tickCallback (node.js:356:17)↵" headers : (d) status : 400 statusText : "Bad Request" __proto__ : Object 

这是我的app.js:

 var express = require('express'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var expressSession = require('express-session'); var path = require('path'); //App init var app = express(); require('./server/config/mongoose.js'); var sessionConfig = { secret:'CookieMonster', // Secret name for decoding secret and such resave:false, // Don't resave session if no changes were made saveUninitialized: true, // Don't save session if there was nothing initialized name:'myCookie', // Sets a custom cookie name cookie: { secure: false, // This need to be true, but only on HTTPS httpOnly:false, // Forces cookies to only be used over http maxAge: 3600000 } } app.use(bodyParser.urlencoded({extended:true})); app.use(bodyParser.json({extended:true})); app.use(expressSession(sessionConfig)); 

我的客户端评论工厂:

 kelvindashdemo.factory('CommentFactory', ['$http', function($http){ var factory = {}; factory.createComment = function(comment, topicId, callback){ $http({ method:"POST", url:"/answers/"+topicId+"/comments/", data:comment }).then(function success(){ callback(); }, function failure(res){ console.log(res); }) } factory.dislike = function(id,callback){ $http({ method:"GET", url:"/comments/dislike/"+id }).then(function success(){ callback(); }, function failure(res){ console.log(res); }) } factory.like = function(id,callback){ $http({ method:"GET", url:"/comments/like/"+id }).then(function success(){ callback(); }, function failure(res){ console.log(res); }) } return factory; }]); 

我的服务器端commentController:

 var mongoose = require('mongoose'); var Answer = mongoose.model('Answer'); var User = mongoose.model('User'); var Comment = mongoose.model('Comment'); module.exports = { createNew: function(req, res){ console.log("HERE"); var commentInfo = req.body; commentInfo._author = req.session.user; commentInfo._answer = req.params.id; var comment = new Comment(commentInfo); comment.save(function(err, comment){ User.update({_id:req.session.user}, {$push:{comments:comment}}, function(err, user){ //pushes comment into user db with id Answer.update({_id:req.params.id}, {$push:{comments:comment}}, function(err, comment){ //pushes comment into topic db with id if (!err){ res.sendStatus(200); }else{ res.sendStatus(400); //can also be a 500 error msg } }); }); }) }, 

最后我的服务器端评论模型:

 var mongoose = require('mongoose'); var CommentSchema = new mongoose.Schema({ comment: {type:String, required:true}, _answer: {type: mongoose.Schema.Types.ObjectId, required:true, ref: 'Answer'}, likes: {type:Number, default:0}, dislikes: {type:Number, default:0}, _author: {type: mongoose.Schema.Types.ObjectId, required:true, ref: 'User'}, }, {timestamps:true}); mongoose.model('Comment', CommentSchema); 

在我的topicController中join以供参考:

 kelvindashdemo.controller('topicsController', ['$scope', 'TopicFactory', '$location', '$routeParams', 'AnswerFactory', 'CommentFactory', function($scope, TopicFactory, $location, $routeParams, AnswerFactory, CommentFactory){ TopicFactory.getTopic($routeParams.id, function(topic){ $scope.topic = topic; console.log(topic); }) $scope.postAnswer = function(answer, topicId){ AnswerFactory.createAnswer(answer, topicId, function(){ TopicFactory.getTopic($routeParams.id, function(topic){ $scope.topic = topic; $scope.answer = {}; console.log(topic); }) }) } $scope.postComment = function(comment, answerId){ CommentFactory.createComment(Comment, answerId, function(){ TopicFactory.getTopic($routeParams.id, function(topic){ $scope.topic = topic; console.log(topic); }) }) } $scope.like = function(id){ CommentFactory.like(id, function(){ TopicFactory.getTopic($routeParams.id, function(topic){ $scope.topic = topic; console.log(topic); }) }) } $scope.dislike = function(id){ CommentFactory.dislike(id, function(){ TopicFactory.getTopic($routeParams.id, function(topic){ $scope.topic = topic; console.log(topic); }) }) } }]) 

任何和所有的反馈是受欢迎的。 我期待在某处听到一个失踪的逗号。

当你调用createComment

 $scope.postComment = function(comment, answerId) { CommentFactory.createComment(Comment, answerId, function(){ TopicFactory.getTopic($routeParams.id, function(topic){ $scope.topic = topic; console.log(topic); }) }) } 

…你通过Comment ,这是没有定义在你发布的任何代码,但我猜这是一种构造函数。 在这个function中:

 factory.createComment = function(comment, topicId, callback){ $http({ method:"POST", url:"/answers/"+topicId+"/comments/", data:comment }).then(function success(){ callback(); }, function failure(res){ console.log(res); }) } 

…第一个参数作为数据发送到$http 。 如果Comment是一个函数,而不是一个可以序列化为JSON的对象,它将被转换为stringfunction Comment() {[native code]} ,这将炸毁服务器上的JSONparsing器(注意它是在抱怨f字符)。

认为你打算做的是传递comment ,而不是Comment当调用CommentFactory.createComment

 $scope.postComment = function(comment, answerId) { CommentFactory.createComment(comment, answerId, function(){ TopicFactory.getTopic($routeParams.id, function(topic){ $scope.topic = topic; console.log(topic); }) }) }