Nodejs报错求助,麻烦各位大仙儿给看看,这个地方为啥报错呢?(菜鸟,第一次写NodeJs)
Nodejs报错求助,麻烦各位大仙儿给看看,这个地方为啥报错呢?(菜鸟,第一次写NodeJs)
function upload(response, request){
console.log(“Request handler ‘upload’ was called.”);
var form = new formidable.IncomingForm();
console.log("About to parse");
form.parse(request, function(error, fields, files){
console.log("parse done");
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src=/show />");
response.end();
});
}
上面是代码,下面是报错的信息。我其实就是在按照《nodeJs入门》那本书在做练习,不知道为啥代码敲完之后,它就报这个错。
fs.renameSync(files.upload.path, "/tmp/test.png");
^
TypeError: Cannot read property 'path' of undefined
at /Users/chenxiaochun/Documents/我的资料/test/nodeJs/requestHandlers.js:36:29
at IncomingForm.<anonymous> (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:118:9)
at IncomingForm.emit (events.js:67:17)
at IncomingForm._error (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:248:8)
at IncomingForm.write (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:145:10)
at IncomingMessage.<anonymous> (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:95:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:113:42)
at Socket.ondata (http.js:1410:22)
at TCP.onread (net.js:374:27)
我把完整的代码贴出来吧,分为以下几个部分。
index.js
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route, handle);
server.js
var http = require("http");
var url = require("url");
function start(route, handle){
function onRequest(request, response){
var postData = "";
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
request.setEncoding("utf8");
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
route.js
function route(handle, pathname, response, request){
console.log("About route a request for " + pathname);
if(typeof handle[pathname] === "function"){
return handle[pathname](response, request);
}else{
console.log("No request handle for " + pathname +".");
response.writeHead("404", {"Content-type": "text/plain"});
response.write("404 not found.");
response.end();
}
}
exports.route = route;
requestHandler.js
var querystring = require("querystring");
var fs = require("fs");
var formidable = require("formidable");
function start(response){
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-type": "text/html"});
response.write(body);
response.end();
}
function upload(response, request){
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("About to parse");
form.parse(request, function(error, fields, files){
console.log("parse done");
console.log(files);
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src='/show' />");
response.end();
});
}
function show(response, postData){
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.png", "binary", function(error, file){
if(error){
response.writeHead(500, {"Content-type": "text/plain"});
response.write(error + "\n");
response.end();
}else{
response.writeHead(200, {"Content-type": "text/plain"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
根据你提供的代码片段和错误信息,问题出在 upload
函数中处理上传文件的部分。具体来说,错误提示 TypeError: Cannot read property 'path' of undefined
表明 files.upload
是 undefined
。
让我们逐步分析并修复这个问题:
问题原因
在 form.parse
的回调函数中,files
参数应该是一个对象,包含了所有上传的文件。如果 files.upload
是 undefined
,可能是因为文件上传过程中出现了问题,或者文件字段名不匹配。
解决方案
-
确保文件字段名正确: 确保你的 HTML 表单中的文件输入字段名与
form.parse
中的fields
和files
参数中的键名一致。 -
检查文件上传是否成功: 在处理文件之前,先打印
files
对象以确认文件是否正确解析。
示例代码
以下是修改后的 upload
函数,包含了一些调试信息:
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("About to parse");
form.parse(request, function(error, fields, files) {
if (error) {
console.error("Error parsing form:", error);
response.writeHead(500, { "Content-Type": "text/html" });
response.write("There was an error processing your request.");
response.end();
return;
}
console.log("parse done");
console.log("Files uploaded:", files);
if (!files.upload || !files.upload.path) {
console.error("File upload failed or no file uploaded.");
response.writeHead(500, { "Content-Type": "text/html" });
response.write("No file was uploaded.");
response.end();
return;
}
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, { "Content-Type": "text/html" });
response.write("Received image:<br>");
response.write("<img src='/show' />");
response.end();
});
}
调试步骤
-
检查表单字段名: 确保你的 HTML 表单中的文件输入字段名与
form.parse
中的fields
和files
参数中的键名一致。例如:<form action="/upload" enctype="multipart/form-data" method="post"> <input type="file" name="upload"> <input type="submit" value="Upload file"> </form>
-
查看日志输出: 运行代码时,注意查看控制台的日志输出,特别是
console.log("Files uploaded:", files);
输出的内容,以确认文件是否正确解析。
通过这些步骤,你应该能够找到并解决文件上传失败的问题。
Cannot read property ‘path’ of undefined 無法讀取 undefined 的 path 屬性,也就是說 files.upload 是undefined。
是,我现在就是不明白它为啥是undefined?
function start(response){
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-type": "text/html"});
response.write(body);
response.end();
}
您说的是这里吗,应该没问题啊。
这是加上您说的request.files.upload之后的upload方法:
function upload(response, request){
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("About to parse");
**console.log(request.files.upload);**
form.parse(request, function(error, fields, files){
console.log("parse done");
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src='/show' />");
response.end();
});
}
可是,运行之后找不到upload属性,下面是报错信息。
console.log(request.files.upload);
^
TypeError: Cannot read property 'upload' of undefined
at Object.upload [as /upload] (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/requestHandlers.js:35:27)
at route (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/router.js:5:25)
at Server.onRequest (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/server.js:13:3)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1514:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1410:22)
at TCP.onread (net.js:374:27)
我是一个初学者,还不会使用您提到的那两个框架。
这是测试代码:
var formidable = require('formidable');
var http = require('http');
var util = require('util');
http.createServer(function(req, res) {
if (req.url == ‘/upload’ && req.method.toLowerCase() == ‘post’) {
// parse a file upload
var form = new formidable.IncomingForm();
form.uploadDir = ‘/home/tmp’;//手动设置默认上传tmp目录,可以通过fs.rename更改
form.parse(req, function(err, fields, files) {
res.writeHead(200, {‘content-type’: ‘text/plain’});
res.write(‘received upload:\n\n’);
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {‘content-type’: ‘text/html’});
res.end(
‘<form action="/upload" enctype=“multipart/form-data” method=“post”>’+
‘<input type=“text” name=“title”><br>’+
‘<input type=“file” name=“upload” multiple=“multiple”><br>’+
‘<input type=“submit” value=“Upload”>’+
‘</form>’
);
}).listen(8080);
然后我上传一个文件后的打印信息是:
received upload:
{ fields: { title: '' },
files:
{ upload:
{ size: 4037,
path: '/home/tmp/0af01b60359775ed9e02c9384cc93dcc',
name: 'status.xsd',
type: 'application/octet-stream',
hash: false,
lastModifiedDate: Thu Aug 09 2012 13:46:21 GMT+0800 (CST),
_writeStream: [Object],
length: [Getter],
filename: [Getter],
mime: [Getter] } } }
想要的信息都有了,你可以通过files.upload取得所有你想要的信息
- 非常感谢您能抽时间给我写这个实例,不过,类似您的这个实例我也已经实现了。
- 我现在是初学者,不仅仅是想实现这个文件上传的功能,其实我更想知道:我上面自己所写的代码,到底是哪里不对,为什么不对。
- 再一次感谢您的帮助。
好吧,用你写的方法,完整代码在此:
var formidable = require('formidable');
var http = require('http');
var util = require('util');
var fs = require('fs');
function upload(response, request){
console.log(“Request handler ‘upload’ was called.”);
var form = new formidable.IncomingForm();
console.log("About to parse");
form.parse(request, function(error, fields, files){
console.log("parse done");
fs.renameSync(files.upload.path, "/home/tmp/test.png");
console.log(files);
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src=/show />");
response.end();
});
}
function start(response){
console.log(“Request handler ‘start’ was called.”);
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-type": "text/html"});
response.write(body);
response.end();
}
http.createServer(function(req, res) {
if (req.url == ‘/upload’ && req.method.toLowerCase() == ‘post’) {
// parse a file upload
upload(res, req);
/*
var form = new formidable.IncomingForm();
form.uploadDir = '/home/tmp';//手动设置默认上传tmp目录,可以通过fs.rename更改
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
*/
}
start(res);
/*
// show a file upload form
res.writeHead(200, {‘content-type’: ‘text/html’});
res.end(
‘<form action="/upload" enctype=“multipart/form-data” method=“post”>’+
‘<input type=“text” name=“title”><br>’+
‘<input type=“file” name=“upload”><br>’+
‘<input type=“submit” value=“Upload”>’+
‘</form>’
);
*/
}).listen(8080);
这个例子我也试过了,开始我是在win7上面,也是这样报错,我就有点怀疑是不是中文的原因,然后我开了虚拟机,在Ubuntu里面装了个,整个路径都是英文名,然后顺利运行了。楼主你也试试看看是不是这个原因
我用的是mac,我已经把整个路径都改成了英文,还是报那个错误。-_-!
因为你的 function(response,request) 是错误的,而应该是 function(request,response) .
form.parse(request, function(error, fields, files){
.....
}
我发现,这个位置,办理出您写的例子files对象,可以看到以下这堆东西:
{ upload:
{ size: 63949,
path: '/tmp/1189fcfbfe44620782b17efb53d23c04',
name: '1.png',
type: 'image/png',
hash: false,
lastModifiedDate: Fri, 10 Aug 2012 02:48:08 GMT,
_writeStream:
{ path: '/tmp/1189fcfbfe44620782b17efb53d23c04',
fd: 9,
writable: false,
flags: 'w',
encoding: 'binary',
mode: 438,
bytesWritten: 63949,
busy: false,
_queue: [],
drainable: true },
length: [Getter],
filename: [Getter],
mime: [Getter] } }
而我写的例子中的files对象仅仅是一个空的{},因此就造成无法找到path。
-
仔细看一下,它代码是这样调用的:
upload(res, req);
因此不可能是这个错误; -
若真弄反了,出错信息应该是
Cannot read property 'files' of undefined
而不是Cannot read property 'path' of undefined
这个问题搞了这么久的主要原因就是上面的第二点,以后记住,回调里面记得首先要判断是否有出错信息,若没有判断,后面会引发的一堆莫名奇妙的错误
刚才闲得蛋疼,上楼主所说的《Node入门》(http://www.nodebeginner.org/index-zh-cn.html )看了一下,人家的代码是这样子的:
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log(“Request for " + pathname + " received.”);
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
console.log(“Server has started.”);
}
exports.start = start;
本以为是《Node入门》误人子弟,原来那句request.setEncoding(“utf8”);
还是楼主自己加上去的,真是
一行代码引发的悲剧
挽楼主尊严哇。
哈哈,,为了debug,可以没有尊严。不过,各位大牛也木有不给尊严啊。
我也是因为马虎啊~~,不过错误地方不一样。 ’<input type=“file” name=“upload” multiple=“multiple”>’(修改后) 提交表单的HTML代码的问题。在win7下完美运行。谢谢各位大牛~~。撒花撒花
遇到了同样的问题,我的原因是没有写 enctype=“multipart/form-data”
根据你的描述和提供的代码片段,错误信息指出 files.upload.path
是 undefined
。这通常意味着表单中的文件字段没有被正确解析或上传。
以下是一些可能的原因及解决方法:
-
确保表单编码类型:在HTML表单中,你需要设置
enctype="multipart/form-data"
,这样表单数据才能被正确解析为二进制格式。 -
检查文件字段名:确保文件输入字段的名称与后端处理时使用的名称一致。例如,在HTML中设置为
<input type="file" name="upload">
,在后端处理时使用files.upload
。 -
确保表单提交到正确的路径:确保表单的
action
属性指向/upload
路径,即后端处理上传逻辑的地方。
以下是一个修复后的示例代码,确保表单字段名和处理逻辑一致:
HTML 表单 (index.html)
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="upload">
<input type="submit" value="Upload file">
</form>
Node.js 代码 (requestHandlers.js
)
const formidable = require('formidable');
const fs = require('fs');
function upload(response, request) {
console.log("Request handler 'upload' was called.");
const form = new formidable.IncomingForm();
console.log("About to parse");
form.parse(request, (error, fields, files) => {
if (error) {
console.error("Error parsing form:", error);
response.writeHead(500, { "Content-Type": "text/html" });
response.write("Error uploading file.");
response.end();
return;
}
console.log("parse done");
if (!files.upload || !files.upload.path) {
console.error("File upload failed or file path is undefined.");
response.writeHead(500, { "Content-Type": "text/html" });
response.write("Failed to upload file.");
response.end();
return;
}
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, { "Content-Type": "text/html" });
response.write("Received image:<br>");
response.write('<img src="/show" />');
response.end();
});
}
// 其他函数保持不变...
以上代码添加了错误处理逻辑,并确保文件路径存在。这样可以避免 TypeError: Cannot read property 'path' of undefined
错误。