我需要上传文件,我在服务器中使用节点js(版本:[nodemon] v1.0.1)。以下代码在4个月之前有效,之后我没有检查它,昨天我再次运行它。但它不起作用,我在控制台中出现"TypeError: Object #<Object> has no method 'existsSync'"
错误。以下是我的代码
var express = require('express');
var path = require('path');
var app = module.exports = express();
var calenderid;
var EventEmitter = require('events').EventEmitter, https = require('https'), http = require('http'), querystring = require('querystring'), url = require('url');
var path2;
app.configure(function() {
app.use(express.compress());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.session({
secret : 'foobar'
}));
app.use(express.bodyParser({
uploadDir : __dirname + '/uploads'
}));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : '3306',
database : 'db',
user : 'root',
password : '',
});
connection.connect();
var fs = require('fs');
app.post('/file-upload/:id', function(req, res) {
var tble = "image";
var id = req.params.id;
console.log("req.files.files" + JSON.stringify(req.files),
req.files.files.length);
var tmp_path = req.files.files[0].path;
console.log("hiii temp path" + tmp_path, typeof tmp_path);
// var firstIdx =0
var lastidx = tmp_path.lastIndexOf("/");
var path = tmp_path.substring(0, lastidx);
console.log("target path sub" + lastidx, tmp_path);
// var target_path = path+"\"+req.files.files[0].name;
var dirExists = fs.existsSync(__dirname + "/uploads/" + id);
console.log(dirExists);
if (!dirExists) {
fs.mkdirSync(__dirname + "/uploads/" + id);
target_path = path + "/" + id + "/" + req.files.files[0].name;
} else {
target_path = path + "/" + id + "/" + req.files.files[0].name;
}
console.log("hiii target_path" + target_path);
fs.rename(tmp_path, target_path, function(err) {
if (err)
throw err;
// delete the temporary file, so that the explicitly set temporary
// upload dir does not get filled with unwanted files
fs.unlink(tmp_path, function() {
if (err)
throw err;
// res.send('File uploaded to: ' + target_path + ' - ' +
// req.files.files[0].size + ' bytes'+req.files.files.length);
// queryurl = 'SELECT * FROM ' +tble + ' WHERE ' + id;
queryurl = 'SELECT * FROM ' + tble + ' WHERE image=' + '"'
+ req.files.files[0].name + '"' + ' AND branch=' + '"' + id
+ '"';
connection.query(queryurl, function(err, result, fields) {
console.log(JSON.stringify(result) + "result.image");
if (result.length == 0) {
console.log("in if !result");
connection.query('INSERT INTO ' + tble + '(' + 'branch'
+ ',' + 'image' + ')' + 'VALUES' + '(' + '"' + id
+ '"' + ',' + '"' + req.files.files[0].name + '"'
+ ")", function(err, result, fields) {
if (err)
throw err;
else {
res.send('File uploaded to: ' + target_path + ' - '
+ req.files.files[0].size + ' bytes'
+ req.files.files.length);
}
});
} else {
console.log("in else !result");
res.send("duplicate");
}
});
});
});
});
以下是我从控制台获得的错误。
TypeError: Object #<Object> has no method 'existsSync'
at /var/www/jts-web/WebContent/app.js:95:20
at callbacks (/var/www/web/WebContent/node_modules/express/lib/router/index.js:165:11)
at param (/var/www/web/WebContent/node_modules/express/lib/router/index.js:139:11)
at param (/var/www/web/WebContent/node_modules/express/lib/router/index.js:136:11)
at pass (/var/www/web/WebContent/node_modules/express/lib/router/index.js:146:5)
at Router._dispatch (/var/www/web/WebContent/node_modules/express/lib/router/index.js:173:5)
at Object.router [as handle] (/var/www/web/WebContent/node_modules/express/lib/router/index.js:33:10)
at next (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:37:5)
at next (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/proto.js:190:15)
在一个网站上有人告诉它在升级节点js后会克服。但是这段代码运行正常。如果我需要为每次更改升级,那就不好了。
答案 0 :(得分:12)
我通过添加以下代码解决了这个问题
fs.existsSync = require('path').existsSync;
以上
var dirExists = fs.existsSync(__dirname + "/uploads/" + id);
答案 1 :(得分:5)
作为旁注,我遇到了一些*类似*问题,但在不同情况下。
我试图为引导程序运行grunt。 这是我得到的错误:
/home/user/gbootstrap/node_modules/grunt/lib/grunt/file.js:374
return fs.existsSync(filepath);
^
TypeError: Object #<Object> has no method 'existsSync'
at Object.exists (/home/user/gbootstrap/node_modules/grunt/lib/grunt/file.js:374:13)
at Task.init (/home/user/gbootstrap/node_modules/grunt/lib/grunt/task.js:423:31)
at Object.initTasks (/home/user/gbootstrap/node_modules/grunt/lib/grunt/help.js:90:14)
at /home/user/gbootstrap/node_modules/grunt/lib/grunt/help.js:49:55
at Array.forEach (native)
at Object.display (/home/user/gbootstrap/node_modules/grunt/lib/grunt/help.js:49:17)
at Object.tasks (/home/user/gbootstrap/node_modules/grunt/lib/grunt.js:101:10)
at Object.cli (/home/user/gbootstrap/node_modules/grunt/lib/grunt/cli.js:38:9)
at Object.<anonymous> (/home/user/gnode-v0.10.26-linux-x86/lib/node_modules/grunt-cli/bin/grunt:45:20)
at Module._compile (module.js:446:26)
我发现根本原因是由于我使用了存储库中的默认debian nodejs包(版本0.6.19)。
解决方案:
1.删除nodejs:
sudo apt-get remove nodejs
2。从http://nodejs.org/download/下载二进制文件 3.使用bin文件夹中的正确节点和npm。然后它运行...
答案 2 :(得分:2)
确保在此文件的顶部加载核心fs
模块,例如var fs = require("fs");
。除此之外,existsSync
应该在那里。
附注:您不能在此类网络服务器中使用同步IO调用。它完全破坏了基本和基础级别的节点并发模型。使用异步版本。你已经使用了其中的一些,只使用异步版本,你就会正确地编码。