2016-10-20 58 views
2

我正在研究一个博客,你可以回答一个问题,人们可以对答案发表评论。询问和回答问题很好。但发表评论不会。 调查后我知道它的JSON相关。可能以某种方式处理身体分析器。也许我错了。花了几个小时比较代码,并找不到错误在哪里。以下是错误和执行console.log:MEAN stack:“SyntaxError:Unexpected token f”Comments not posting

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

我的客户端commentFactory:

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

我的服务器端commentController:

var mongoose = require('mongoose'); 
 
var Answer = mongoose.model('Answer'); 
 
var User = mongoose.model('User'); 
 
var Comment = mongoose.model('Comment'); 
 

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

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

var mongoose = require('mongoose'); 
 

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

 
mongoose.model('Comment', CommentSchema);
加入我topicController以供参考:

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

任何和所有的反馈是值得欢迎的。我期待着在某个地方听到它缺少逗号。

+0

不是100%肯定,但检查该行: '_author:{类型:mongoose.Schema .Types.ObjectId,required:true,ref:'User'},}' 不确定你需要那里的逗号。 还从来没有看到一个封闭的 ']' 从这一行:'kelvindashdemo.factory( 'CommentFactory',[ '$ HTTP',函数($ HTTP){' –

+0

我会检查HTTP请求的样子在网络中你的开发工具选项卡。 – Jacob

+0

最有可能的,并不会发送有效的JSON什么是你传递给'createComment' – Jacob

回答

0

当你调用createComment

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

...你传递Comment,这是不以任何你发布的代码的定义,但我猜这是一个某种构造函数。在该功能:

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的对象,则它将转换为字符串function Comment() {[native code]},这将炸毁服务器上的JSON解析器(注意它正在抱怨遇到f字符)。

认为你的意思做的是调用CommentFactory.createComment时候传球comment,不Comment

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

雅各布,谢谢。就是这样。我已经走了很多次这条路,应该已经抓住了这个道路。我欠你一杯先生。保重。 PS ..我upvoted这个,但因为我是新的它不会记录它,直到其他事情。 – LANole

+0

没问题。当有人提供如此有用的细节时,这是一件乐事。 – Jacob

相关问题