I'm doing middleware module that will extract data from redis and put to req.my_session.[here]
This is function that call inside app.use();
function parse_cookies(req){
if(req.headers.cookie != null){
var result = req.headers.cookie.match(new RegExp('m:[^=]*=[^; ]*', 'ig'));
if(result != null){
for(var i = 0; i < result.length; i++){
var result1 = result[i].split('=');
req.my_session[result1[0].substr(2)] = result1[1];
// get from redis value
client.get('sess:'+result1[1], function(err, reply) {
// reply is null when the key is missing
console.log(reply);
let li = i;
req.my_session[result1[0].substr(2)] = reply;
console.log('li = ' + li);
console.log('result1.lenght= ' + result.length);
if(i == result.length){
console.log('call the next');
}
});
}
}
}
} // parse_cookies
in console i outputs always 3, how can I get all data from database using redis.get and on last data call next() function for get out from my function?
problem it's get data from database in my middleware, I can't because redis has callback function
client.get("missingkey", function(err, reply) {
// reply is null when the key is missing
console.log(reply);
});
I think the issue is becuase of async in loop you can try the following
function parse_cookies(req){
if(req.headers.cookie != null){
var result = req.headers.cookie.match(new RegExp('m:[^=]*=[^; ]*', 'ig'));
if(result != null){
var promises = [];
for(var i = 0; i < result.length; i++){
var result1 = result[i].split('=');
promises.push(getFromRd(req, result1));
}
return Promise.all(promises)
.then(() => {
return next()
})
.catch((e) => {
return next({error: e})
})
}
}
} // parse_cookies
function getFromRd(req, result1) {
req.my_session[result1[0].substr(2)] = result1[1];
// get from redis value
return client.get('sess:'+result1[1], function(err, reply) {
if (err) {
throw Error(' failed to find ' + 'sess:' + result1[1])
}
// reply is null when the key is missing
console.log(reply);
let li = i;
req.my_session[result1[0].substr(2)] = reply;
console.log('li = ' + li);
console.log('result1.lenght= ' + result.length);
return {success:true}
});
}
Related
How can I make the while loop with switch-case synchronous in nodejs? Below is the code I am trying to iterate for 2 iterations of the while loop. But the problem is the next iteration gets iterated even before completing all the synchronous functions here which are readdirSync, readFileSync, etc.
What I want is with each whileloop iteration it should run all the methods and then again for next iteration.
Currently the output is:
i = 0
i = 1
Below is my code:
const dir4Data = 'C:/Users/em/Desktop/Regex_test_case/data/google_forms';
const dir4Polls = 'C:/Users/em/Desktop/Regex_test_case/Poll_Report';
var dir, i=0;
var regex4Data = /^\d{1,2}([./-])\d{1,2}\1\d{4}$/;
var regex4Polls = /^[1-9]\d*$/;
while(i < 2)
{
switch (i) {
case 0:
dir = dir4Data;
break;
case 1:
dir = dir4Polls;
break;
}
console.log('i = ', i);
fs.readdirSync(dir, function(err,files){ //Get a listing of all the files in the dir
if (err) throw err;
files.forEach(function(file){
//console.log("file = "+ file);
const writePath = 'C:/Users/em/Desktop/Regex_test_case/output/'+file;
fs.readFileSync(dir+'/'+file, function(err, data) {
console.log("data = "+ data);
var csvContent = data.toString().split('\n'); // read file and convert to array by line break
csvContent.forEach((item, index) => {
var csv;
if(!regex4Data.test(item.toString().split(' ')[0]) && index != 0 && i == 0) {
csv = csvContent[index-1].replace(/(\r\n|\n|\r)/gm, ""); // remove the breaks
// console.log(index, ' Replaced', csv);
fs.appendFileSync(writePath, csvContent[index].toString()+ ' ', function (err, data) {
if (err) throw err;
//console.log('Saved!');
})
}
else if(!regex4Polls.test(item.toString().split(',')[0]) && index != 0 && i == 1) {
csv = csvContent[index-1].replace(/(\r\n|\n|\r)/gm, ""); // remove the breaks
// console.log(index, ' Replaced', csv);
fs.appendFileSync(writePath, csvContent[index].toString()+ ' ', function (err, data) {
if (err) throw err;
//console.log('Saved!');
})
}
else {
csv = item.toString();
fs.appendFileSync(writePath, "\n"+csv + " ", function (err, data) {
if (err) throw err;
//console.log('Saved!');
})
}
});
})
})
})
i++;
}
There are two main error in your script.
First of all, you are using wrong the FS module function.
From the doc, https://nodejs.org/api/fs.html#fs_synchronous_example, all the FS Sync function are synchronous. That means that you don't have to pass them a callback.
You can just write
var response = fs.readFileSync(path);
And the value of response is your file.
The other thing is, if you wont to iterate an async function, you cannot with callback, but you can do it using async/await.
Here an example of loop working:
async function loop() {
var i = 0
while (i < 2) {
var response = await fs.promises.readFile(path);
console.log(response)
i++
}
}
I'm using redis and nodejs for the first time, without success. Trying to loop insert data but got an empty array.
const redis = require("redis");
const client = redis.createClient({
retry_strategy: function(options) {
if (options.error && options.error.code === "ECONNREFUSED") {
// End reconnecting on a specific error and flush all commands with
// a individual error
return new Error("The server refused the connection");
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands
// with a individual error
return new Error("Retry time exhausted");
}
if (options.attempt > 10) {
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.min(options.attempt * 100, 3000);
},
});
var data_value = {
id: '235235',
totalrv: 'WAIT',
product: 'productName2',
url: 'url2',
process: 'proc',
task: 'msg'
};
client.set("key0", JSON.stringify(data_value));
client.set("key1", JSON.stringify(data_value));
client.set("key2", JSON.stringify(data_value));
client.set("key3", JSON.stringify(data_value));
client.set("key4", JSON.stringify(data_value));
//client.get("key2", redis.print);
var logger_data = {
logger: []
};
client.keys('*', function (err, keys) {
if (err) return console.log(err);
for(var i = 0, len = keys.length; i < len; i++) {
var values_v = client.get(keys[i].toString(), function(err, val) {
// console.log(logger_data);
// data is exist ...
logger_data.logger.push(JSON.parse(val));
});
}
});
// empty data
console.log(logger_data);
I wrote 2 print data result, in the loop it's working ok, but end of function, there are no data in array.
you can call a function inside the callback if you want to print the logger_data with values outside the asynchronous callback, like this
function printLoggerData(){
console.log(logger_data);
}
client.keys('*', function (err, keys) {
if (err) return console.log(err);
for(var i = 0, len = keys.length; i < len; i++) {
var values_v = client.get(keys[i].toString(), function(err, val) {
// console.log(logger_data);
// data is exist ...
logger_data.logger.push(JSON.parse(val));
// calling the function here so that it contains the latest values
printLoggerData();
});
}
});
Ok, with help of CertainPerformance, it's now working in asynchronous.
Thank you very much ...
async function getMessage () {
var logger_data = {
logger: []
};
return new Promise(function(resolve, reject) {
client.keys('*', function (err, keys) {
if (err) return console.log(err);
for(var i = 0, len = keys.length; i < len; i++) {
var values_v = client.get(keys[i].toString(), function(err, val) {
logger_data.logger.push(JSON.parse(val));
resolve(logger_data);
});
}
});
});
}
async function main() {
let message = await getMessage();
console.log(message);
}
main();
I am trying to finish my script, but for some reason i don't know, it refuses to execute in the order i put it in.
I've tried placing a 'wait' function between the JoinRequest update function and the following code, but when run, it acts as if the function call and wait function were the other way round, countering the point of the wait().
const Roblox = require('noblox.js')
var fs = require('fs');
var joinRequests = []
...
function wait(ms) {
var d = new Date();
var d2 = null;
do { d2 = new Date(); }
while(d2-d < ms*1000);
};
...
function updateJReqs() {
Roblox.getJoinRequests(4745601).then((array) => {
var i;
var final = [];
for(i = 0; i < array.length; i++) {
final.push(array[i].username);
};
if(final === '') {
final = '-None';
};
joinRequests = final
console.log('Updated join requests.')
});
}
function check() {
setTimeout(() => {
fs.readFile('Request.txt',encoding = 'utf-8', function(err, data) {
if (err) {
check();
} else {
updateJReqs(); //for some reason this function is executed alongside the below, not before it.
// Tried putting wait(x) in here.
console.log('Request received: ' + data)
var solution = joinRequests
console.log('Fuffiling request with ' + solution)
fufillRequest(solution)
fs.unlink('Request.txt', function(err) {
if(err) throw err;
});
check();
}
});
}, 400)
}
check();
The script is supposed to wait until a file is created (accomplished), update the list of join requests (accomplished) and then create a new file with the list of join requests in(not accomplished).
if I understand your code you work with async code, you need to return a promise in updateJReqs and add a condition of leaving from the function because you have an infinite recursion
function updateJReqs() {
return new Promise(resolve => {
Roblox.getJoinRequests(4745601).then((array) => {
var i;
var final = [];
for(i = 0; i < array.length; i++) {
final.push(array[i].username);
};
if(final === '') {
final = '-None';
};
joinRequests = final
console.log('Updated join requests.')
resolve();
});
}
}
async function check() {
setTimeout(() => {
fs.readFile('Request.txt',encoding = 'utf-8', function(err, data) {
if (err) {
await check();
} else {
await updateJReqs();
// Tried putting wait(x) in here.
console.log('Request received: ' + data)
var solution = joinRequests
console.log('Fuffiling request with ' + solution)
fufillRequest(solution)
fs.unlink('Request.txt', function(err) {
if(err) throw err;
});
// you dont have an exit from your function check();
return 'Success';
}
});
}, 400)
}
check().then(res => console.log(res));
In node.js + sqlite3:
What is a good way to notify a caller from a callback about errors and get the caller to try again ? When we get a database locked error - would like to try and run the query again.
TestController.prototype.addDevices = function (number_of_devices, callback_after_all_devices_have_been_added) {
var controller = this;
var db = controller.connectToDb();
for(i = 0; i < number_of_devices; i++) {
db.each("SELECT api_key as ak FROM user_table ORDER BY RANDOM() LIMIT 1", function(err, row) {
//sometimes we get a SQLITE: database locked error
if (err !== null) {
console.log("Error found");
//i-- this will not work - but how do we do it then?
return;
}
console.log("Error: " + err);
//code to process if we get an entry from the user_table ..
});
}
}
This is how I managed to solve this - added a retry counter and moved the problem code to its own function, passing in the epilogue as a callback. It works - but is this a good way?
(other than using promises or the async module?)
TestController.prototype.addDevices = function (number_of_devices, callback_after_all_devices_have_been_added) {
var number_of_users = 0;
var controller = this;
controller.pending_db_writes_for_device = number_of_devices;
for(i = 0; i < number_of_devices; i++) {
var api_key = controller.getRandomAPIKeyFromDB(10,function(access_key) {
var mac_address = controller.generateRandomMAC();
var friendly_name = Faker.random.array_element(["Washing Machine","Television","Dishwasher","Set top box"]);
var description = Faker.Lorem.sentence(5);
controller.registerDeviceWithAPI(mac_address, friendly_name, description, access_key, callback_after_all_devices_have_been_added);
});
}
}
TestController.prototype.getRandomAPIKeyFromDB = function (retry_counter, callback) {
var controller = this;
var db = controller.connectToDb();
console.log("Retry counter: "+ retry_counter);
db.each("SELECT api_key as ak FROM user_table ORDER BY RANDOM() LIMIT 1", function(err, row) {
if (err !== null) {
console.log("Error: " + err + "retry count: "+ retry_counter);
console.log("Error found");
if(retry_counter === 0) {
console.log("Bailing out after retry limit reached");
return;
}
controller.getRandomAPIKeyFromDB(--retry_counter,callback);
}
else {
console.log("Successfully got api key: " + row.ak);
callback(row.ak);
}
}
);
}
I have to following function that assesses if a password is in the user's password history. Here is the code:
isPasswordInPwdHistory : function(redisClient, userId, encPassword, cb) {
var key = 'user:' + userId + ':pwdhistory';
redisClient.llen(key, function(err, reply) {
if (err) {
status.results = [ ];
xutils.addStatusResultsItem(status.results, err, null, null, null);
cb(status);
}
else {
var numPwds = reply;
var match = false;
var funcError;
for (i=0; i <= numPwds - 1; i++) {
var error;
var pwdValue;
redisClient.lindex(key, i, function(err, reply) {
if (err) {
console.log('lindex err = ' + err);
error = err;
}
else {
console.log('lindex reply = ' + reply);
console.log('lindex encPassword = ' + encPassword);
console.log('lindex (encPassword === reply) = ' + (encPassword === reply));
pwdValue = reply;
}
});
console.log('for-loop error = ' + error);
console.log('for-loop pwdValue = ' + pwdValue);
console.log('for-loop (encPassword === pwdValue) = ' + (encPassword === pwdValue));
if (error) {
funcError = error;
break;
}
else if (encPassword === pwdValue) {
console.log('passwords match');
match = true;
break;
}
}
console.log('funcError = ' + funcError);
console.log('match = ' + match);
if (funcError) {
status.results = [ ];
xutils.addStatusResultsItem(status.results, err, null, null, null);
cb(status);
}
else
cb(match);
}
});
}
Here is the console output:
for-loop error = undefined
for-loop pwdValue = undefined
for-loop (encPassword === pwdValue) = false
funcError = undefined
match = false
isPasswordInPwdHistory = false
lindex reply = 5f4f68ed57af9cb064217e7c28124d9b
lindex encPassword = 5f4f68ed57af9cb064217e7c28124d9b
lindex (encPassword === reply) = true
Once I leave the scope of the redisClient.lindex() call I lose the values. How can I pass these values for evaluation in the for-loop?
UPDATE
I refactored the code a bit to handle the matching of the new password (encPassword) vs. the existing password at index i when the redisClient.lindex() callback is issued.
isPasswordInPwdHistory : function(redisClient, userId, encPassword, cb) {
var status = new Object();
var key = 'user:' + userId + ':pwdhistory';
redisClient.llen(key, function(err, reply) {
if (err) {
status.results = [ ];
xutils.addStatusResultsItem(status.results, err, null, null, null);
cb(status);
}
else {
var numPwds = reply;
var loopCt = 0;
for (i=0; i <= numPwds - 1; i++) {
loopCt++;
redisClient.lindex(key, i, function(err, reply) {
if (err) {
status.results = [ ];
xutils.addStatusResultsItem(status.results, err, null, null, null);
cb(status);
}
else if (encPassword === reply) {
status.results = [ ];
xutils.addStatusResultsItem(status.results, null, 0, null, true);
cb(status);
}
else if (loopCt === numPwds && encPassword !== reply) {
status.results = [ ];
xutils.addStatusResultsItem(status.results, null, 0, null, false);
cb(status);
}
});
}
}
});
}
Unfortunately, caller sees status === undefined even though encPassword === reply is true and I issue the cb(status). How can I exit the for-loop for good after issuing cb(status)?
It looks like you have a mix of synchronous and asynchronous thinking in your logic. Your for loop is going to fire off requests to Redis as fast as it can, without waiting for Redis to respond (this is the nature of asynchronous IO). So, by the time your code runs console.log('funcError = ' + funcError); and console.log('match = ' + match);, Redis likely hasn't even responded for the very first value of i in the loop (which matches what you're finding in your output).
I would probably look into a library like async to help with this task; in particular, whilst looks like it might be a good fit. Perhaps something like:
var numPwds = reply;
...
var match = false;
var i = 0;
async.whilst(
// keep looping until we have a match or we're done iterating the list
function () { return match == false && i < numPwds; },
// this is our function to actually check Redis
function (callback) {
redisClient.lindex(key, i, function(err, reply) {
if (err) {
console.log('lindex err = ' + err);
error = err;
}
else {
if (encPassword === reply) { match = true; } // this will cause `whilst` to stop.
}
i++;
callback();
});
},
function (err) {
// this is called when our first function that calls
// return match == false && i < numPwds
// returns false, which will happen when we're out of list elements to check
// or we have a match. At this point, match being true or false
// let us know if we found the password.
}
);
As a final note, unless you need to support the same password showing up multiple times in a the "used passwords" list, you can save yourself a lot of heartache by using a set or a sorted set.