gpt4 book ai didi

javascript - Node js : TypeError: Object # has no method 'existsSync'
转载 作者:搜寻专家 更新时间:2023-10-31 23:28:21 26 4
gpt4 key购买 nike

我需要上传文件,我在服务器中使用 Node 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)

在一个站点有人告诉它会在升级 Node js 后克服。但是这段代码运行良好。如果每次更改都需要升级,那就不好了。

最佳答案

我通过添加以下代码解决了这个问题

fs.existsSync = require('path').existsSync;

以上

var dirExists = fs.existsSync(__dirname + "/uploads/" + id); 

I got solution from this site

关于javascript - Node js : TypeError: Object #<Object> has no method 'existsSync' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22005002/

26 4 0