I am trying to write a file copy code in Node.js:
function archiveFolder(site, callback) {
//console.log("Processing Site2 " + site );
var path = srcDir + '/' + site;
var startDate = moment(new Date(), 'YYYYMMDD');
asyncCounter++;
console.log("getting list of files for site " + site);
fse.readdir(path, function(err, files) {
console.log("got list of files for site " + site);
if (err) {
console.log(err);
return callback(err);
}
async.eachLimit(files, 5, function(file, callback2) {
var endDate = moment(file, 'YYYYMMDD');
var diff = startDate.diff(endDate, 'days');
fs.stat(path + '/' + file, function(err, stats) {
try {
if (err) {
return callback2(err);
}
if (/^\d{8}$/i.test(file) && stats.isDirectory() && diff >= 7) {
fse.ensureDir(destDir + '/' + site, function(err) {
fse.copy(path + '/' + file, destDir + '/' + site + '/' + file, function(err) {
try {
if (err) {
//console.log(err);
return callback2(err);
} else {
console.log("copied " + site + '/' + file);
return callback2();
}
} catch (e) {
console.log(e);
return callback2(err);
}
});
});
} else {
// //console.log("failed " + path + '/' + file)
return callback2();
}
} catch (e) {
console.log(e);
return callback2(err);
}
});
}, function(err) {
if (!err) {
//console.timeEnd("site");
console.log("finished Site " + site + " asyncCounter " + asyncCounter);
return callback();
} else {
return callback(err + " test");
}
});
});
}
Still there isn't any error message on console or the system logs /var/log/messages
What could be the reason for this behavior? I can increase the ulimit on the server but I want to confirm that this is due to this "number of files open" limit.
Related
I want to download a zip file using express, the point here is that by when I am trying to download txt file it is working correctly it downloads what I want. But in case of .zip format it downloads an empty file with the exact name what I want but in txt format.
static getFile(req, res) {
fileManager.getfile(req, (r) => {
if (!r.success) {
return res.status(r.code).send(r);
}
return res.download(r.result.zipFileDirName + r.result.zipFileName);
});
}
this is the get file functuion
getFile(req, callback) {
let validation = reportValidator.validateGetFile(req);
if (!validation.success) {
return callback(validation);
}
let findQuery = validation.result.query;
Something.find(findQuery).exec((err, gh) => {
if (err) {
logger.error(`Error while finding somehting, err : ${err}`);
return callback({success: false, code: 500, err: err});
}
let dirName = parentDir + req.query.accountId + '_' + req.query.dateFrom + '_' + req.query.dateTo + '/';
let archive = archiver('zip', {
gzip: true,
zlib: { level: 9 }
});
archive.on('error', (err) => {
logger.error(err);
return callback({success: false, code: 500, err: err});
});
archive.on('end', () => {
console.log('Archive wrote %d bytes', archive.pointer());
});
let zipFileName = req.query.accountId + '_' + req.query.dateFrom + '_' + req.query.dateTo + '.zip';
let zipFile = fs.createWriteStream(dirName + zipFileName);
archive.pipe(zipFile);
async.each(gh,
(item, next) => {
let fileName = item.handId + '_' + Date.now() + '.mat';
this.makeReportingFile(dirName, fileName, item.gameInfo, r => {
if (!r.success) {
return next(err);
}
archive.file(r.result.file, {name: item.handId + '.mat'});
next(null, 'OK');
});
},
(err) => {
if (err) {
logger.error(JSON.stringify(err));
return callback({success: false, code: 500, err: err});
}
archive.finalize();this on end
return callback({success: true, code: 200, result: {message: "OK", zipFileDirName: dirName, zipFileName: zipFileName}});
});
});
}
I have provided additional info
I'm trying to get the data from LDAP and I'm getting it successfully but it's not written into variable so then after the code is executed I can make some checks on the data.
var server = LdapJS.createClient({
url: LdapConf.server.url,
tlsOptions: LdapConf.server.tlsOptions
});
server.bind(LdapConf.server.bindDN, LdapConf.server.bindCredentials, function(err) {
if (err) {
return done(err);
}
});
var SearchOtps = {
filter: '(uid=' + username + ')',
scope: 'one',
};
var UserSearch = server.search(LdapConf.server.searchBase, SearchOtps, function(err, res) {
res.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
return (JSON.stringify(entry.object));
});
res.on('searchReference', function(referral) {
//console.log('referral: ' + referral.uris.join());
});
res.on('error', function(err) {
//console.error('error: ' + err.message);
});
res.on('end', function(result) {
//console.log('status: ' + result.status);
});
});
console.log(UserSearch);
I just do not know how to stop further code execution while it's waiting for the return of the LDAP search.
Server Started
undefined
You could do a function that returns it in promise.
function UserSearch(server,LdapConf, SearchOtps) {
return new Promise(function(resolve,reject) {
server.search(LdapConf.server.searchBase, SearchOtps, function(err, res) {
res.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
resolve(JSON.stringify(entry.object)));
});
res.on('searchReference', function(referral) {
//console.log('referral: ' + referral.uris.join());
});
res.on('error', function(err) {
reject()
});
res.on('end', function(result) {
//console.log('status: ' + result.status);
});
});
}
}
UserSearch(server,LdapConf, SearchOtps)
.then(function(res) {
console.log(res)
})
I'm concatenating tweets from a defined user through a helper file and trying to retrieve it in my server.js but there the str value is still undefined (and this line gets executed first), then the console.log from my helper prints with the right value.
Output:
GET /login/twitter/callback 302 618.242 ms - 0
Concatenated Tweets in Server: undefined
Concatenated Tweets in Helper: Test Tweet 3 #TestTweet Test Tweet 2
Test Tweet 1
Can anyone help on what control flow I should use to call twitterHelper.getTweets functions to get the returned str in the server please? Thanks!
Server.js
app.get('/login/twitter/callback',
passport.authenticate('twitter', {failureRedirect: "/login"},
function(req, res) {
// auth success
async.waterfall ([
function(callback) {
callback(null, twitterHelper.getTweets(user));
},
function(str, callback) {
console.log("Concatenated Tweets in Server: " + str);
callback(null);
}
],
function(err) {
if(err)
console.log("Error: " + err);
}
);
}
)
);
Helper.js
var concatTweets = '';
var promise = new Promise(
function(resolve, reject) {
T.get('statuses/user_timeline', params, function( err, data, response) {
if(err)
reject(err);
else {
for (var i = 0; i < data.length ; i++)
concatTweets = concatTweets + " " + data[i].text;
resolve(concatTweets);
}
})
}
).then(
str => {
console.log("Concatenated Tweets in Helper: " + str);
return str;
}, err => {
console.log(err);
return err;
}
);
Instead of using this longway you can use this simple way by promise.
Helper.js
var concatTweets = '';
var getTweets = function(user){
var promise = new Promise(function(resolve, reject) {
T.get('statuses/user_timeline', params, function( err, data, response) {
if(err){
reject(err);
} else {
for (var i = 0; i < data.length ; i++)
concatTweets = concatTweets + " " + data[i].text;
console.log("Concatenated Tweets in Helper: " + concatTweets);
resolve(concatTweets);
}
})
});
return promise;
}
Server.js
app.get('/login/twitter/callback', passport.authenticate('twitter', {failureRedirect: "/login"},function(req, res) {
// auth success
twitterHelper.getTweets(user).then(str=>{
console.log("Concatenated Tweets in Server: " + str);
}).catch(err=>{
console.log("Error: " + err);
});
}));
I hope this will work for you.
in my server application i have some method such as:
insert message when user joined to application(insertUserJoinedMessageAndSendMessage)
get unique id of owner mobile numbers (getOwnerPhoneNumberUniqueIds)
print generated data
in this solution i have:
insertUserJoinedMessageAndSendMessage(userPhoneNumbers, assignedUserId.id)
.then(function (t) {
return getOwnerPhoneNumberUniqueIds(userPhoneNumbers);
})
.then(function (result) {
log.info(result);
})
.catch(function (error) {
log.info(error);
});
methods, insertUserJoinedMessageAndSendMessage method work fine without any problem, i want to get generated data on nested Promise as getUserId() from getOwnerPhoneNumberUniqueIds() Promise function, but i can't print data on this part of code as log.info(result); because i can't return array from getOwnerPhoneNumberUniqueIds()
function insertUserJoinedMessageAndSendMessage(userPhoneNumbers, ownerUserId) {
return new Promise(function (resolve, reject) {
let query = "INSERT INTO `userJoinedMobileNumbers` (`id`, `userId`, `nameAndFamily`, `mobileNumber`, `created_at`, `updated_at`) VALUES ";
userPhoneNumbers.map(function (curr) {
query += "(NULL, " + ownerUserId + ", " + curr.contactName + ", " + curr.contactPhone + ", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),";
});
query = query.substr(0, query.length - 1) + ";";
connection.query(query, function (err, results) {
if (err) return reject(err);
resolve(true);
});
});
}
function getOwnerPhoneNumberUniqueIds(data) {
return Promise.all(data.map(function (curr) {
return getUserId(curr.contactPhone)
.then(function (data) {
log.info("1) " + data);
return {id: data};
});
})).then(function (accountNumbers) {
log.info("2) " + accountNumbers);
return accountNumbers
}).catch(function (err) {
console.log(err);
});
}
function getUserId(contactPhone) {
return new Promise(function (resolve, reject) {
var query = "SELECT id FROM sendMessageUserJoined WHERE `phoneNumber` ='" + contactPhone + "'";
connection.query(query, function (err, results) {
if (err) return reject(err);
if (results.length > 0) {
log.info("0) " + results[0].id);
resolve(results[0].id);
}
});
});
}
in log.info("0) " + results[0].id); i get result of sql command and that return id successful to getOwnerPhoneNumberUniqueIds() and i can print result with this part of code as log.info("1) " + data);, now how can i return that on:
.then(function (accountNumbers) {
log.info("2) " + accountNumbers);
return accountNumbers
})
log.info("2) " + accountNumbers);
dont print result which returned by return {id: data};
The problem is in getUserId.
In some cases you wouldn't resolve or reject the promise. So your Promise.all returned promise maintained pending.
function getUserId(contactPhone) {
return new Promise(function (resolve, reject) {
var query = "SELECT id FROM sendMessageUserJoined WHERE `phoneNumber` ='" + contactPhone + "'";
connection.query(query, function (err, results) {
if (err) return reject(err);
if (results.length > 0) {
log.info("0) " + results[0].id);
resolve(results[0].id);
}
else { //this part is missing
resolve(); //or reject
}
});
});
}
I'm new to NodeJS and I#m trying to read a directory recursively this is my code
var fs = require('fs');
var readDir = function (dir, calback) {
fs.readdir(dir, function (err, files) {
if (err)
console.log(err);
for (var file in files) {
fs.stat(dir + "/" + file, function (err, stats) {
if (err)
console.log(err);
if (stats.isFile()) {
calback(dir + "/" + file);
}
if (stats.isDirectory()) {
walk(file, calback);
}
});
}
});
};
This is my ErrorMessage
C:\Users\Lukas\Desktop\Enide-Studio-05-kepler-win32\ws\PlayerTest\hello-world-server.js:24
if (fs.stats.isFile()) {
^
TypeError: Cannot call method 'isFile' of undefined
at C:\Users\Lukas\Desktop\Enide-Studio-05-kepler-win32\ws\PlayerTest\hello-world-server.js:24:30
at Object.oncomplete (fs.js:107:15)
What is my mistake??
You are not exiting your function after encountering an error.
fs.stat(dir + "/" + file, function (err, stats) {
if (err) {
console.log(err);
return; // exit here since stats will be undefined
}
if (stats.isFile()) {
calback(dir + "/" + file);
}
if (stats.isDirectory()) {
walk(file, calback);
}
});