请教一下,这个js报错,Cannot set property 'num' js typeof undefinedd

解决discuz与jquery不兼容问题——下拉列表不能用(Uncaught TypeError: Cannot set property position of undefined)
问题描述:
最近学着用discuz来模仿一个门户网站,里面要把导航条做出滑动效果,要用到jquery,将要用到jquery引用到header.htm中,如下图所示:
但是由于discuz采用php以及本身的机制,jquery中的$会与discuz本身的common.js和forum.js产生冲突,会出现如下的错误导致下拉列表
解决办法:
解决思路:将jquery的$重新定义一下,比如我门将用jq代表jquery的$
在jquery-1.7.2.js的末尾加上一句如下代码:
var jq = $.noConflict();//使用 noConflict() 方法为 jQuery 变量规定新的名称
如图所示:
因为我header.htm中也用了$,将其改成jq,如下所示:
同时要将引用到的其他js中的$改成jq,改动jquery.movebg.js文件如下图所示:
这样问题就解决了,如下图所示:
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
网站后台的编辑器使用的是ckeditor for java,版本号为3.3.2.先前运行很正常,没有出现问题,但是今天突然发现,当使用chrome插入图片时,选择完图片,点击确定按钮时,会报: Uncaught TypeError: Cannot call method 'moveToPosition' of undefined 的错误,而使用ie就没有问题 ...
最近在做表单校验的时候,自己写的addMethod 方法老是不起作用,折腾了接近一天的时间.报的错误如下 Uncaught TypeError: Cannot read property 'call' of undefinedjquery.validate.min.js:28 先来看一下 我自己页面的布局. top.jsp 上面的截图为top.jsp的头部引 ...
本周用jquery file upload这个插件做一个上传文件的功能,打开页面的时候浏览器控制台报错: Uncaught TypeError: Cannot call method 'unshift' of undefined 而且连报3个同样的,分别来自: jquery.fileupload-image.js jquery.fileupload-audi ...
Uncaught TypeError: Object #&Object& has no method 'tmpl'这个是因为使用模板页的时候饮用 &script type=&text/javascript& src=&~/Scripts/jquery.tmpl.min.js&&&/scri ...
Uncaught TypeError: Cannot call method 'createChild' of undefined 我在使用Ext 4.1.1做grid.Panel,然后chrome爆出这么个错误,无法理解,百度谷歌终于改好了 是这样子的,grid.Panel的整个代码要放到Ext.onReady里面,例如 Ext.onReady(funct ...
Common.js是官方自带的,我没改过它,所以不可能出错,所以问题就应该是两者冲突了.导致的结果我也发现了,前台的DIY功能不能用了.
我还花了很多时间在那里研究冲突的地方,后来还是没能解决.之后我去网上搜索“Discuz! jQuery”,果然有文章写两者冲突的事情.其实19楼那里下载下来的js就是一个jQuery,只是后面外加了一些function ...
最近把一个项目的jQuery升级到最新版,发现有些页面报错Cannot read property ‘msie’ of undefined.上jQuery网站上搜了一下,原因是$.browser这个api从jQuery1.9开始就正式废除,js代码里只要用到$.browser就会报这个错.具体说明参见jQuery官方说明. 撸主顺便扩展阅读了一下,发现jQu ...
前言: 现在越来越多的朋友喜欢将jQuery和ASP.NET AJAX一起来用,最近,许多印度人在forum里抱怨说UpdatePanel和jQuery不兼容,许多jQuery效果在UpdatePanel更新之后就不work.本篇通过分析两者的框架结构来解决这个兼容性问题. 问题重现: 1. 在页面中添加ScriptManager和UpdatePanel 2 ...echarts地图扩展js报错:Uncaught TypeError: Cannot read property &getGeoJson& of undefined - 开源中国社区
当前访客身份:游客 [
当前位置:
代码如下:
&head runat="server"& &&& &title&&/title& &&& &script src="echarts/jquery.min.js" type="text/javascript"&&/script& &&& &script src="echarts/esl.js" type="text/javascript"&&/script& &&& &script src="echarts/echarts-plain-map.js" type="text/javascript"&&/script& &&&
&/head& &body& &&& &div id="main" style="height:800border:1px solid #padding:10"&&/div& &&& &script type="text/javascript"& &&&&&&& echarts.util.mapData.params.HK = {&&&&&&&
&&&&&&&&&&& getGeoJson: function (callback) { &&&&&&&&&&&&&&& $.getJSON('getJson/HK_geo.json', callback); &&&&&&&&&&& } &&&&&&& }; &&&&&&& var myChart = echarts.init(document.getElementById('main')); &&&&&&& option = { &&&&&&&&&&& title: { &&&&&&&&&&&&&&& text: '香港18区人口密度 (2011)', &&&&&&&&&&&&&&& subtext: '人口密度数据来自Wikipedia', &&&&&&&&&&&&&&& sublink: 'http://zh.wikipedia.org/wiki/%E9%A6%99%E6%B8%AF%E8%A1%8C%E6%94%BF%E5%8D%80%E5%8A%83#cite_note-12' &&&&&&&&&&& }, &&&&&&&&&&& tooltip: { &&&&&&&&&&&&&&& trigger: 'item', &&&&&&&&&&&&&&& formatter: '{b}&br/&{c} (p / km2)' &&&&&&&&&&& }, &&&&&&&&&&& toolbox: { &&&&&&&&&&&&&&& show: true, &&&&&&&&&&&&&&& orient: 'vertical', &&&&&&&&&&&&&&& x: 'right', &&&&&&&&&&&&&&& y: 'center', &&&&&&&&&&&&&&& feature: { &&&&&&&&&&&&&&&&&&& mark: { show: true }, &&&&&&&&&&&&&&&&&&& dataView: { show: true, readOnly: false }, &&&&&&&&&&&&&&&&&&& restore: { show: true }, &&&&&&&&&&&&&&&&&&& saveAsImage: { show: true } &&&&&&&&&&&&&&& } &&&&&&&&&&& }, &&&&&&&&&&& dataRange: { &&&&&&&&&&&&&&& min: 800, &&&&&&&&&&&&&&& max: 50000, &&&&&&&&&&&&&&& text: ['High', 'Low'], &&&&&&&&&&&&&&& realtime: false, &&&&&&&&&&&&&&& calculable: true, &&&&&&&&&&&&&&& color: ['orangered', 'yellow', 'lightskyblue'] &&&&&&&&&&& }, &&&&&&&&&&& series: [ &&&&&&& { &&&&&&&&&&& name: '香港18区人口密度', &&&&&&&&&&& type: 'map', &&&&&&&&&&& mapType: 'HK', // 自定义扩展图表类型 &&&&&&&&&&& itemStyle: { &&&&&&&&&&&&&&& normal: { label: { show: true} }, &&&&&&&&&&&&&&& emphasis: { label: { show: true} } &&&&&&&&&&& }, &&&&&&&&&&& data: [ &&&&&&&&&&&&&&& { name: '中西区', value: 20057.34 }, &&&&&&&&&&&&&&& { name: '湾仔', value: 15477.48 }, &&&&&&&&&&&&&&& { name: '东区', value: 31686.1 }, &&&&&&&&&&&&&&& { name: '南区', value: 6992.6 }, &&&&&&&&&&&&&&& { name: '油尖旺', value: 44045.49 }, &&&&&&&&&&&&&&& { name: '深水埗', value: 40689.64 }, &&&&&&&&&&&&&&& { name: '九龙城', value: 37659.78 }, &&&&&&&&&&&&&&& { name: '黄大仙', value: 45180.97 }, &&&&&&&&&&&&&&& { name: '观塘', value: 55204.26 }, &&&&&&&&&&&&&&& { name: '葵青', value: 21900.9 }, &&&&&&&&&&&&&&& { name: '荃湾', value: 4918.26 }, &&&&&&&&&&&&&&& { name: '屯门', value: 5881.84 }, &&&&&&&&&&&&&&& { name: '元朗', value: 4178.01 }, &&&&&&&&&&&&&&& { name: '北区', value: 2227.92 }, &&&&&&&&&&&&&&& { name: '大埔', value: 2180.98 }, &&&&&&&&&&&&&&& { name: '沙田', value: 9172.94 }, &&&&&&&&&&&&&&& { name: '西贡', value: 3368 }, &&&&&&&&&&&&&&& { name: '离岛', value: 806.98 } &&&&&&&&&&& ], &&&&&&&&&&& // 自定义名称映射 &&&&&&&&&&& nameMap: { &&&&&&&&&&&&&&& 'Central and Western': '中西区', &&&&&&&&&&&&&&& 'Eastern': '东区', &&&&&&&&&&&&&&& 'Islands': '离岛', &&&&&&&&&&&&&&& 'Kowloon City': '九龙城', &&&&&&&&&&&&&&& 'Kwai Tsing': '葵青', &&&&&&&&&&&&&&& 'Kwun Tong': '观塘', &&&&&&&&&&&&&&& 'North': '北区', &&&&&&&&&&&&&&& 'Sai Kung': '西贡', &&&&&&&&&&&&&&& 'Sha Tin': '沙田', &&&&&&&&&&&&&&& 'Sham Shui Po': '深水埗', &&&&&&&&&&&&&&& 'Southern': '南区', &&&&&&&&&&&&&&& 'Tai Po': '大埔', &&&&&&&&&&&&&&& 'Tsuen Wan': '荃湾', &&&&&&&&&&&&&&& 'Tuen Mun': '屯门', &&&&&&&&&&&&&&& 'Wan Chai': '湾仔', &&&&&&&&&&&&&&& 'Wong Tai Sin': '黄大仙', &&&&&&&&&&&&&&& 'Yau Tsim Mong': '油尖旺', &&&&&&&&&&&&&&& 'Yuen Long': '元朗' &&&&&&&&&&& }, &&&&&&&&&&& // 文本位置修正 &&&&&&&&&&& textFixed: { &&&&&&&&&&&&&&& 'Yau Tsim Mong': [-10, 0] &&&&&&&&&&& }, &&&&&&&&&&& // 文本直接经纬度定位 &&&&&&&&&&& geoCoord: { &&&&&&&&&&&&&&& 'Islands': [113.95, 22.26] &&&&&&&&&&& } &&&&&&& } &&& ] &&&&&&& }; &&& myChart.setOption(option); &&& &/script& &/body&
运行时会出现js错误:Uncaught TypeError: Cannot read property 'getGeoJson' of undefined!
新手初学,求前辈帮忙。。。
共有1个答案
<span class="a_vote_num" id="a_vote_num_
echarts.util.mapData.params.HK 改&echarts.util.mapData.params.params.HK
--- 共有 1 条评论 ---
成功了,感谢
(2年前)&nbsp&
更多开发者职位上
有什么技术问题吗?
类似的话题[jQuery] Cannot read property &#8216;msie&#8217; of undefined错误的解决方法 &#8211; blchen的空间
Categories
Recent Posts麻烦各位大仙儿给看看,这个地方为啥报错呢?(菜鸟,第一次写nodeJs) - CNode技术社区
这家伙很懒,什么个性签名都没有留下。
function upload(response, request){
console.log(&Request handler &#x27;upload&#x27; 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, &&#x2F;tmp&#x2F;test.png&);
response.writeHead(200, {&Content-type&: &text&#x2F;html&});
response.write(&Received image:&br&&);
response.write(&&image src=&#x2F;show &#x2F;&&);
response.end();
上面是代码,下面是报错的信息。我其实就是在按照《nodeJs入门》那本书在做练习,不知道为啥代码敲完之后,它就报这个错。
fs.renameSync(files.upload.path, &&#x2F;tmp&#x2F;test.png&);
TypeError: Cannot read property &#x27;path&#x27; of undefined
at &#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;requestHandlers.js:36:29
at IncomingForm.&anonymous& (&#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;node_modules&#x2F;formidable&#x2F;lib&#x2F;incoming_form.js:118:9)
at IncomingForm.emit (events.js:67:17)
at IncomingForm._error (&#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;node_modules&#x2F;formidable&#x2F;lib&#x2F;incoming_form.js:248:8)
at IncomingForm.write (&#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;node_modules&#x2F;formidable&#x2F;lib&#x2F;incoming_form.js:145:10)
at IncomingMessage.&anonymous& (&#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;node_modules&#x2F;formidable&#x2F;lib&#x2F;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)
我把完整的代码贴出来吧,分为以下几个部分。
var server = require(&.&#x2F;server&);
var router = require(&.&#x2F;router&);
var requestHandlers = require(&.&#x2F;requestHandlers&);
var handle = {};
handle[&&#x2F;&] = requestHandlers.
handle[&&#x2F;start&] = requestHandlers.
handle[&&#x2F;upload&] = requestHandlers.
handle[&&#x2F;show&] = requestHandlers.
server.start(router.route, handle);
var http = require(&http&);
var url = require(&url&);
function start(route, handle){
function onRequest(request, response){
var postData = &&;
var pathname = url.parse(request.url).
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 =
function route(handle, pathname, response, request){
console.log(&About route a request for & + pathname);
if(typeof handle[pathname] === &function&){
return handle[pathname](response, request);
console.log(&No request handle for & + pathname +&.&);
response.writeHead(&404&, {&Content-type&: &text&#x2F;plain&});
response.write(&404 not found.&);
response.end();
exports.route =
requestHandler.js
var querystring = require(&querystring&);
var fs = require(&fs&);
var formidable = require(&formidable&);
function start(response){
console.log(&Request handler &#x27;start&#x27; was called.&);
var body = &#x27;&html&&#x27;+
&#x27;&head&&#x27;+
&#x27;&meta http-equiv=&Content-Type& &#x27;+
&#x27;content=&text&#x2F; charset=UTF-8& &#x2F;&&#x27;+
&#x27;&&#x2F;head&&#x27;+
&#x27;&body&&#x27;+
&#x27;&form action=&&#x2F;upload& enctype=&multipart&#x2F;form-data& &#x27;+
&#x27;method=&post&&&#x27;+
&#x27;&input type=&file& name=&upload&&&#x27;+
&#x27;&input type=&submit& value=&Upload file& &#x2F;&&#x27;+
&#x27;&&#x2F;form&&#x27;+
&#x27;&&#x2F;body&&#x27;+
&#x27;&&#x2F;html&&#x27;;
response.writeHead(200, {&Content-type&: &text&#x2F;html&});
response.write(body);
response.end();
function upload(response, request){
console.log(&Request handler &#x27;upload&#x27; 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, &&#x2F;tmp&#x2F;test.png&);
response.writeHead(200, {&Content-type&: &text&#x2F;html&});
response.write(&Received image:&br&&);
response.write(&&image src=&#x27;&#x2F;show&#x27; &#x2F;&&);
response.end();
function show(response, postData){
console.log(&Request handler &#x27;show&#x27; was called.&);
fs.readFile(&&#x2F;tmp&#x2F;test.png&, &binary&, function(error, file){
if(error){
response.writeHead(500, {&Content-type&: &text&#x2F;plain&});
response.write(error + &\n&);
response.end();
response.writeHead(200, {&Content-type&: &text&#x2F;plain&});
response.write(file, &binary&);
response.end();
exports.start =
exports.upload =
exports.show =
Cannot read property ‘path’ of undefined 無法讀取 undefined 的 path 屬性,也就是說 files.upload 是undefined。
是,我现在就是不明白它为啥是undefined?
查看你的request,然后看files,再看你页面上file标签的name是不是对
function start(response){
console.log(&Request handler &#x27;start&#x27; was called.&);
var body = &#x27;&html&&#x27;+
&#x27;&head&&#x27;+
&#x27;&meta http-equiv=&Content-Type& &#x27;+
&#x27;content=&text&#x2F; charset=UTF-8& &#x2F;&&#x27;+
&#x27;&&#x2F;head&&#x27;+
&#x27;&body&&#x27;+
&#x27;&form action=&&#x2F;upload& enctype=&multipart&#x2F;form-data& &#x27;+
&#x27;method=&post&&&#x27;+
&#x27;&input type=&file& name=&upload&&&#x27;+
&#x27;&input type=&submit& value=&Upload file& &#x2F;&&#x27;+
&#x27;&&#x2F;form&&#x27;+
&#x27;&&#x2F;body&&#x27;+
&#x27;&&#x2F;html&&#x27;;
response.writeHead(200, {&Content-type&: &text&#x2F;html&});
response.write(body);
response.end();
您说的是这里吗,应该没问题啊。
你在upload方法里,看下req里有没有files属性,有的话用 var file = req.files.即可取到文件
还有,你使用的express还是connect?
这是加上您说的request.files.upload之后的upload方法:
function upload(response, request){
console.log(&Request handler &#x27;upload&#x27; 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, &&#x2F;tmp&#x2F;test.png&);
response.writeHead(200, {&Content-type&: &text&#x2F;html&});
response.write(&Received image:&br&&);
response.write(&&image src=&#x27;&#x2F;show&#x27; &#x2F;&&);
response.end();
可是,运行之后找不到upload属性,下面是报错信息。
console.log(request.files.upload);
TypeError: Cannot read property &#x27;upload&#x27; of undefined
at Object.upload [as &#x2F;upload] (&#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;requestHandlers.js:35:27)
at route (&#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;router.js:5:25)
at Server.onRequest (&#x2F;Users&#x2F;chenxiaochun&#x2F;Documents&#x2F;我的资料&#x2F;test&#x2F;nodeJs&#x2F;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)
我是一个初学者,还不会使用您提到的那两个框架。
呃,这样说好累,你直接用的formidable吧,我试下先
呃,是好累,可我也没啥好办法。不过,还是非常感谢哈。。。
这是测试代码:
var formidable = require(&#x27;formidable&#x27;);
var http = require(&#x27;http&#x27;);
var util = require(&#x27;util&#x27;);
http.createServer(function(req, res) {
if (req.url == &#x27;&#x2F;upload&#x27; && req.method.toLowerCase() == &#x27;post&#x27;) {
&#x2F;&#x2F; parse a file upload
var form = new formidable.IncomingForm();
form.uploadDir = &#x27;&#x2F;home&#x2F;tmp&#x27;;&#x2F;&#x2F;手动设置默认上传tmp目录,可以通过fs.rename更改
form.parse(req, function(err, fields, files) {
res.writeHead(200, {&#x27;content-type&#x27;: &#x27;text&#x2F;plain&#x27;});
res.write(&#x27;received upload:\n\n&#x27;);
res.end(util.inspect({fields: fields, files: files}));
&#x2F;&#x2F; show a file upload form
res.writeHead(200, {&#x27;content-type&#x27;: &#x27;text&#x2F;html&#x27;});
&#x27;&form action=&&#x2F;upload& enctype=&multipart&#x2F;form-data& method=&post&&&#x27;+
&#x27;&input type=&text& name=&title&&&br&&#x27;+
&#x27;&input type=&file& name=&upload& multiple=&multiple&&&br&&#x27;+
&#x27;&input type=&submit& value=&Upload&&&#x27;+
&#x27;&&#x2F;form&&#x27;
}).listen(8080);
然后我上传一个文件后的打印信息是:
received upload:
{ fields: { title: &#x27;&#x27; },
{ size: 4037,
path: &#x27;&#x2F;home&#x2F;tmp&#x2F;0af01be02c9384cc93dcc&#x27;,
name: &#x27;status.xsd&#x27;,
type: &#x27;application&#x2F;octet-stream&#x27;,
hash: false,
lastModifiedDate: Thu Aug 09 :21 GMT+0800 (CST),
_writeStream: [Object],
length: [Getter],
filename: [Getter],
mime: [Getter] } } }
想要的信息都有了,你可以通过files.upload取得所有你想要的信息
非常感谢您能抽时间给我写这个实例,不过,类似您的这个实例我也已经实现了。
我现在是初学者,不仅仅是想实现这个文件上传的功能,其实我更想知道:我上面自己所写的代码,到底是哪里不对,为什么不对。
再一次感谢您的帮助。
好吧,用你写的方法,完整代码在此:
var formidable = require(&#x27;formidable&#x27;);
var http = require(&#x27;http&#x27;);
var util = require(&#x27;util&#x27;);
var fs = require(&#x27;fs&#x27;);
function upload(response, request){
console.log(&Request handler &#x27;upload&#x27; 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, &&#x2F;home&#x2F;tmp&#x2F;test.png&);
console.log(files);
response.writeHead(200, {&Content-type&: &text&#x2F;html&});
response.write(&Received image:&br&&);
response.write(&&image src=&#x2F;show &#x2F;&&);
response.end();
function start(response){
console.log(&Request handler &#x27;start&#x27; was called.&);
var body = &#x27;&html&&#x27;+
&#x27;&head&&#x27;+
&#x27;&meta http-equiv=&Content-Type& &#x27;+
&#x27;content=&text&#x2F; charset=UTF-8& &#x2F;&&#x27;+
&#x27;&&#x2F;head&&#x27;+
&#x27;&body&&#x27;+
&#x27;&form action=&&#x2F;upload& enctype=&multipart&#x2F;form-data& &#x27;+
&#x27;method=&post&&&#x27;+
&#x27;&input type=&file& name=&upload&&&#x27;+
&#x27;&input type=&submit& value=&Upload file& &#x2F;&&#x27;+
&#x27;&&#x2F;form&&#x27;+
&#x27;&&#x2F;body&&#x27;+
&#x27;&&#x2F;html&&#x27;;
response.writeHead(200, {&Content-type&: &text&#x2F;html&});
response.write(body);
response.end();
http.createServer(function(req, res) {
if (req.url == &#x27;&#x2F;upload&#x27; && req.method.toLowerCase() == &#x27;post&#x27;) {
&#x2F;&#x2F; parse a file upload
upload(res, req);
var form = new formidable.IncomingForm();
form.uploadDir = &#x27;&#x2F;home&#x2F;tmp&#x27;;&#x2F;&#x2F;手动设置默认上传tmp目录,可以通过fs.rename更改
form.parse(req, function(err, fields, files) {
res.writeHead(200, {&#x27;content-type&#x27;: &#x27;text&#x2F;plain&#x27;});
res.write(&#x27;received upload:\n\n&#x27;);
res.end(util.inspect({fields: fields, files: files}));
start(res);
&#x2F;&#x2F; show a file upload form
res.writeHead(200, {&#x27;content-type&#x27;: &#x27;text&#x2F;html&#x27;});
&#x27;&form action=&&#x2F;upload& enctype=&multipart&#x2F;form-data& method=&post&&&#x27;+
&#x27;&input type=&text& name=&title&&&br&&#x27;+
&#x27;&input type=&file& name=&upload&&&br&&#x27;+
&#x27;&input type=&submit& value=&Upload&&&#x27;+
&#x27;&&#x2F;form&&#x27;
}).listen(8080);
这个例子我也试过了,开始我是在win7上面,也是这样报错,我就有点怀疑是不是中文的原因,然后我开了虚拟机,在Ubuntu里面装了个,整个路径都是英文名,然后顺利运行了。楼主你也试试看看是不是这个原因
我用的是mac,我已经把整个路径都改成了英文,还是报那个错误。-_-!
你们还真不幸。。。
初学,很有可能就是一个低级的小错误。
因为你的 function(response,request) 是错误的,而应该是 function(request,response) .
console.log(files) 看看
form.parse(request, function(error, fields, files){
我发现,这个位置,办理出您写的例子files对象,可以看到以下这堆东西:
{ size: 63949,
path: &#x27;&#x2F;tmp&#x2F;1189fcfbfeefb53d23c04&#x27;,
name: &#x27;1.png&#x27;,
type: &#x27;image&#x2F;png&#x27;,
hash: false,
lastModifiedDate: Fri, 10 Aug :08 GMT,
_writeStream:
{ path: &#x27;&#x2F;tmp&#x2F;1189fcfbfeefb53d23c04&#x27;,
writable: false,
flags: &#x27;w&#x27;,
encoding: &#x27;binary&#x27;,
mode: 438,
bytesWritten: 63949,
busy: false,
_queue: [],
drainable: true },
length: [Getter],
filename: [Getter],
mime: [Getter] } }
而我写的例子中的files对象仅仅是一个空的{},因此就造成无法找到path。
不是吧,我不是最后给出了用‘你写的代码’的例子
提交表单的时候,你有没有选择一个文件啊?
-_-!,当然选择了,我选择一个图片,然后点击上传,浏览器给我的返回结果是无法连接到localhost:8888
仔细看一下,它代码是这样调用的:upload(res, req); 因此不可能是这个错误;
若真弄反了,出错信息应该是Cannot read property 'files' of undefined而不是Cannot read property 'path' of undefined
刚测试过,没有选择文件,直接点“提交”按钮确实会提示这个出错。
那如果选择了文件呢?能上传成功,并且显示出来吗?
是,您写的例子确实是用了一部分“我的代码”,而且运行起来也是正确的,我一直在拿你写的这段代码和我的代码进行比对,但我实在是看不出来,到底是哪里错了。-_-!!!
是,我也测试过了,确实不是这个地方的原因。
输出了files,它仅仅是一个空的{},不知道为什么。
额滴神那,肯定是有地方错了呗,看看分号啊,参数顺序啊,监听端口啊,formidable版本啊,文件编码啊等等等等。。。
哈哈,是。您说的这些地方 ,我也一直在检查,这不是看不出问题所在嘛。如果我的水平让您抓狂了,实在是报歉。
这倒没有。实在不行先放放呗,这段代码都很简单,先用正确的,不用纠结了
Request handler &#x27;upload&#x27; was called.
About to parse
Request handler &#x27;start&#x27; was called.
parse done
{ size: 71040,
path: &#x27;\\tmp\\d8f69e1c65b5db83acdb1af&#x27;,
name: &#x27;php.ini&#x27;,
type: &#x27;application&#x2F;octet-stream&#x27;,
lastModifiedDate: Fri Aug 10 :00 GMT+0800 (中国标准时间),
_writeStream:
{ path: &#x27;\\tmp\\d8f69e1c65b5db83acdb1af&#x27;,
writable: false,
flags: &#x27;w&#x27;,
encoding: &#x27;binary&#x27;,
mode: 438,
bytesWritten: 71040,
busy: false,
_queue: [],
_open: [Function],
drainable: true },
length: [Getter],
filename: [Getter],
mime: [Getter] } }
Request handler &#x27;start&#x27; was called.
是,你说的有道理。但是心里堵着个疙瘩,实在是不爽。
所以你贴这个代码没有用的,问题不在于这里,而在于files在哪里生成的。
您贴个完整的代码出来看看吧。。。
还有一个可能是你提交表单的HTML代码可能不正确,表单属性必须是enctype=&multipart/form-data&才可以上传文件的
我已经检查了,是这个类型的。
我已经在上面把完整的源码贴出来了,麻烦您帮忙看一下。
这是我写的实例的运行结果吗,竟然可以上传成功。
问题解决了:
在文件server.js中,你不应该用request.setEncoding(&utf8&);来手动设置字符编码,当你设置了这个之后,会导致formidable解析出错;
在文件requestHandler.js中,form.parse(request, function(error, fields, files){这里没有判断是否有出错信息error,因为若error不为null,则fileds和files就可能不能得到正确的值;
这也解释了为什么
给出的精简代码能正确运行,而你这一陀代码却会会出错。
这个问题搞了这么久的主要原因就是上面的第二点,以后记住,回调里面记得首先要判断是否有出错信息,若没有判断,后面会引发的一堆莫名奇妙的错误
老雷牛bi。。。
刚才闲得蛋疼,上楼主所说的《Node入门》( )看了一下,人家的代码是这样子的:
var http = require(&http&);
var url = require(&url&);
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).
console.log(&Request for & + pathname + & received.&);
route(handle, pathname, response, request);
http.createServer(onRequest).listen(8888);
console.log(&Server has started.&);
exports.start =
本以为是《Node入门》误人子弟,原来那句request.setEncoding(&utf8&);还是楼主自己加上去的,真是
一行代码引发的悲剧
两眼热泪啊,感激之情,无以言表。果然是犯了一个愚蠢的错误。-_-!!!
挽楼主尊严哇。
哈哈,,为了debug,可以没有尊严。不过,各位大牛也木有不给尊严啊。
我也是因为马虎啊~~,不过错误地方不一样。
’&input type="file" name="upload" multiple="multiple"&’(修改后)
提交表单的HTML代码的问题。在win7下完美运行。谢谢各位大牛~~。撒花撒花
遇到了同样的问题,我的原因是没有写 enctype=“multipart/form-data”
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
服务器赞助商为
,存储赞助商为
,由提供应用性能服务。
新手搭建 Node.js 服务器,推荐使用无需备案的}

我要回帖

更多关于 js怎么判断undefined 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信