Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  46] [ 7]  / answers: 1 / hits: 18773  / 11 Years ago, tue, february 25, 2014, 12:00:00

I need to upload file and I am using node js in server(version : [nodemon] v1.0.1). The following code worked before 4 months after that I did n't check it and yesterday I run it again. But it is not working and I got TypeError: Object #<Object> has no method 'existsSync' error in console. Following is my code



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);

}
});

});
});

});


Following is the error that I got from console.



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)


In one site somebody told it will overcome after upgrading node js. But this code was working fine. It is not good if I need to upgrade for each change.


More From » node.js

 Answers
2

I solved this issue by adding the following code



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


above



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


I got solution from this site


[#72327] Sunday, February 23, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaya

Total Points: 531
Total Questions: 107
Total Answers: 100

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
;