我正在研究一个博客,你可以回答一个问题,人们可以对答案发表评论。询问和回答问题很好。但发表评论不会。 调查后我知道它的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> 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));
我的客户端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);
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 }
}])
任何和所有的反馈是值得欢迎的。我期待着在某个地方听到它缺少逗号。
不是100%肯定,但检查该行: '_author:{类型:mongoose.Schema .Types.ObjectId,required:true,ref:'User'},}' 不确定你需要那里的逗号。 还从来没有看到一个封闭的 ']' 从这一行:'kelvindashdemo.factory( 'CommentFactory',[ '$ HTTP',函数($ HTTP){' –
我会检查HTTP请求的样子在网络中你的开发工具选项卡。 – Jacob
最有可能的,并不会发送有效的JSON什么是你传递给'createComment' – Jacob