当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - MEAN stack: "SyntaxError: Unexpected token f" Comments not posting

问题描述:

I am working on a blog where you can answer a question and people can comment on the answer. Asking and answering the question works fine. But posting a comment does not.

After investigating I know its JSON related. Probably dealing with body-parser in some way. Maybe I'm wrong. Have spent hours comparing code and can't find where the error is. Here is the error and the 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

Here is my 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));

网友答案:

When you're calling createComment:

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

...you're passing Comment, which isn't defined in any of the code you've posted, but I'm guessing it is a constructor function of some sort. Within that 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);
    })  
}

...the first argument is sent as the data to $http. If Comment is a function, not an object that can be serialized to JSON, it is getting converted to the string function Comment() {[native code]}, which would blow up the JSON parser on the server (notice it's complaining about encountering the f character).

I think what you meant to do is to pass comment, not Comment when calling CommentFactory.createComment:

$scope.postComment = function(comment, answerId) {
  CommentFactory.createComment(comment, answerId, function(){           
    TopicFactory.getTopic($routeParams.id, function(topic){         
      $scope.topic = topic;
      console.log(topic);           
    })
  })
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: