我正在写博客,您可以回答问题,人们可以对答案发表评论。询问和回答这个问题很好。但发表评论却没有。 调查后我知道它的JSON相关。可能以某种方式处理body-parser。也许我错了。花了几个小时比较代码,无法找到错误的位置。这是错误和console.log:
POST http://localhost:8000/answers/5807c9ef24adc7ea591a35b1/comments/ 400 (Bad Request)
Object {data: "SyntaxError: Unexpected token f<br> a… at process._tickCallback (node.js:356:17)↵", status: 400, config: Object, statusText: "Bad Request"}
config
:
Object
data
:
"SyntaxError: Unexpected token f<br> at parse (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\lib\types\json.js:83:15)<br> at C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\lib\read.js:116:18<br> at invokeCallback (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\node_modules\raw-body\index.js:262:16)<br> at done (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\node_modules\raw-body\index.js:251:7)<br> at IncomingMessage.onEnd (C:\Users\US\Documents\coding\KelvinDashDemo\node_modules\body-parser\node_modules\raw-body\index.js:307:7)<br> at emitNone (events.js:67:13)<br> at IncomingMessage.emit (events.js:166:7)<br> at endReadableNT (_stream_readable.js:921:12)<br> at nextTickCallbackWith2Args (node.js:442:9)<br> 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));
&#13;
我的客户端commentFactory:
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;
}]);
&#13;
我的服务器端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
}
});
});
})
},
&#13;
最后我的服务器端评论模型:
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);
&#13;
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);
})
})
}
}])
&#13;
欢迎提出任何反馈意见。我期待在某个地方听到一个丢失的逗号。
答案 0 :(得分: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
字符)。
我认为你打算做的是在调用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);
})
})
}