错误:解析器错误,已解析 0 个字节(共 4344 个字节)(Node.js)
我正在尝试使用上传程序来上传我的文件。我使用的代码是
app.post('/photos',loadUser, function(req, res) {
var post = new Post();
req.form.complete(function(err, fields, files) {
if(err) {
console.log(err);
next(err);
} else {
ins = fs.createReadStream(files.file.path);
ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
post.filename=files.file.filename;
post.file=files.file.path;
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else {
post.save(function(err,docs) {
req.flash('info', 'information Saved');
res.redirect('/photos');
});
}
});
}
});
});
当我删除 loadUser 方法时,一切工作正常,但是当我使用 loadUser
方法时,它给了我一个错误。错误的控制台信息是:
Error: parser error, 0 of 4344 bytes parsed
at IncomingForm.write (/home/darhamid/node_modules/formidable/lib/incoming_form.js:141:17)
at IncomingMessage.<anonymous> (/home/darhamid/node_modules/formidable/lib/incoming_form.js:91:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:121:23)
at Socket.ondata (http.js:1349:22)
at TCP.onread (net_uv.js:312:27)
仅当我使用 loadUser 函数时才会导致该错误,如果我删除 loadUser 函数,则一切正常。 我不知道这背后的原因并且被困住了。有人可以帮我吗?
I am trying to use an upload program to upload my files. The code that I use is
app.post('/photos',loadUser, function(req, res) {
var post = new Post();
req.form.complete(function(err, fields, files) {
if(err) {
console.log(err);
next(err);
} else {
ins = fs.createReadStream(files.file.path);
ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
post.filename=files.file.filename;
post.file=files.file.path;
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else {
post.save(function(err,docs) {
req.flash('info', 'information Saved');
res.redirect('/photos');
});
}
});
}
});
});
When I remove loadUser method everything is working fine, but when I use the loadUser
method it is giving me an error. The console information of the error is:
Error: parser error, 0 of 4344 bytes parsed
at IncomingForm.write (/home/darhamid/node_modules/formidable/lib/incoming_form.js:141:17)
at IncomingMessage.<anonymous> (/home/darhamid/node_modules/formidable/lib/incoming_form.js:91:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:121:23)
at Socket.ondata (http.js:1349:22)
at TCP.onread (net_uv.js:312:27)
The error is caused only when i use loadUser function, if i remove the loadUser Funciton everything is working fine.
I don't know the reason behind this and am stuck. Can anyone help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您试图在一切之前执行数据库操作,这给您带来了问题。尝试以下代码:
祝你好运...
You are trying to perform database operation before everything, which is creating problems for you. Try the following code:
Good luck...
请参阅此 github 问题: https://github.com/felixge/node-formidable/issues/ 34
See this github issue : https://github.com/felixge/node-formidable/issues/34
该问题的另一个可能原因是这一行:
request.setEncoding( "utf8" );
Another possible cause for the problem is in this line:
request.setEncoding( "utf8" );