- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个博客,您可以在其中回答问题,人们可以对答案发表评论。提问和回答问题效果很好。但发表评论则不然。经过调查我知道它与 JSON 相关。可能以某种方式处理主体解析器。也许我错了。花了几个小时比较代码,找不到错误所在。这是错误和 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));
我的客户端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;
}]);
我的服务器端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);
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
,它在您发布的任何代码中都没有定义,但我猜测它是某种构造函数。在该函数中:
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);
})
})
}
关于javascript - 平均堆栈 : "SyntaxError: Unexpected token f" Comments not posting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40164851/
我在 Python 2 中尝试了这段代码: def NewFunction(): return '£' 但我收到一条错误消息: SyntaxError: Non-ASCII character
我正在学习xpath,并且我正尝试从html usint xpath获取一些数据 我发现谷歌浏览器可以选择“复制xpath”,效果很好 但不适用于这个例子 some divs
我是 ruby 的初学者。我想修复其中一个邮件软件中的错误。我应用的修复代码如下: @headers[:recipient] = { "To" => (cc.map do |p|
我是初学者级别的python用户,当我在终端中键入以下内容时: $ pydoc Inleesgenbank.py 我收到以下错误消息: ./Inleesgenbank.py中的问题-:语法无效(Inl
我正在编写用于解析电子邮件的脚本,但是以下部分的for循环上有一些SyntaxError: def main(): writer = csv.DictWriter(open('feature
我正在尝试在python33中创建分发文件,但没有成功。 我用嵌套器名称创建了一个文件夹,并在Windows 8的C驱动程序中放入了python33。 此文件夹有2个文件。 nester.py和set
当我尝试导入NumPy时,突然出现以下错误: 更具体地说,它在我键入时显示: import numpy as np 要不就: import numpy 它也会在Python控制台中发生,如下所示: P
在我的 HTML 文件中,我有一行(如下)通过 WiFi 从设备获取响应并使数据可用于我的 JavaScript,它运行良好,除非响应文本中有错误并停止。 响应是一个代表 JavaScript 变量的
我开始使用 Javascript OOP,我编写了我的第一个类,但我在控制台中收到消息错误 这是类(class): class Quote{ deleteQuote(callback){ $(
我正在使用argparse来解析参数,但是当我得到args.global时,出现了一个奇怪的错误,我不知道我哪里做错了 ... parser.add_argument('-u','--update',
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在使用argparse来解析参数,但是当我得到args.global时,出现了一个奇怪的错误,我不知道我哪里做错了 ... parser.add_argument('-u','--update',
有什么不同?为什么它会在函数 a() 中出错? function a(){ 1 == 1 ? return true: ""; // Uncaught SyntaxError: Unexpe
我有一个 python 脚本,其中包含如下函数参数的类型声明: def dump_var(v: Variable, name: str = None): 据我所知,这是一个为函数设置输入参数类型的有效
我正在尝试从命令行运行 Python 脚本,这是我的脚本: import sys def printsomething(sys.argv): text = str(sys.argv[1])
我在 macbook 上使用终端将数据打印到打开的文件中: >>> out=open("test_output.txt","w") >>> print("hello",file=out) File
我想排除以下代码产生的错误,但我不知道如何。 from datetime import datetime try: date = datetime(2009, 12a, 31) except:
我想在动态生成的列表中放置一个 onclick 事件。我不能按原样使用它,例如 updateRoomID(arg) ,因为它会立即开火。所以我把它放在一个匿名函数中,按照网上各种来源的建议:funct
我有以下脚本: 测试.py: import sys try: import random print random.random() except: print sys.exc
这个问题在这里已经有了答案: "+=" causing SyntaxError in Python (6 个答案) 关闭 3 年前。 在我的代码中有这些行: if numVotes == 0:
我是一名优秀的程序员,十分优秀!