I'm writing a policy checkSession which basically destroy session after some time.
checkSession.js (policies/checksession.js)
var moment = require('moment');
function checkLastActivity(req) {
if (!req.session.lastActivity) {
return false; // no session.
} else {
return true;
}
}
function setActivity(req) {
req.session.lastActivity = moment(new Date()).format();
return true;
}
function updateActivity(req, res, updated) {
req.session.lastActivity = moment(new Date()).format();
updated(null, {
isdestroted: 0
});
}
function checkIdleTime(req, idleTime) {
try {
var lastActivityTime = moment(req.session.lastActivity).format();
var currentActivityTime = moment(new Date()).format();
timeIdle = moment.utc(moment(currentActivityTime).diff(moment(lastActivityTime))).format("HH:mm:ss");
minutestimeIdle = moment.duration(timeIdle).asMinutes().toFixed(2);
idleTime(null, minutestimeIdle);
} catch (e) {
idleTime(e, null);
}
}
function destroyActivity(req, res, destroyed) {
try {
req.session.destroy();
sails.log.info("***********************session is expired for user *****************");
destroyed(null, {
isdestroted: 1,
redirectCode: 200,
redirectView: '/login/'
});
} catch(e) {
destroyed(e, null);
}
}
module.exports = function isSession(req, res, next) {
cleanParamsService.cleanPrm(req);
async.waterfall([
function(callback) {
callback(null, checkLastActivity(req));
},
function(isLastActivity, callback) {
if (isLastActivity) {
checkIdleTime(req, function(err, timeIdle) {
if (minutestimeIdle > sails.config.session_timeout) { /*if user is being idle*/
destroyActivity(req, res, function(err, destroyed) {
sails.log.warn('User was idle since' + minutestimeIdle + ', sessions are destroyed');
callback(null, destroyed);
})
} else {
updateActivity(req, res, function(err, updated) {
callback(null, updated);
});
}
})
} else {
updateActivity(req, res, function(err, updated) {
callback(null, updated);
});
}
}
], function(err, result) {
if (result) {
if (result.isdestroted == 0) {
return next();
} else {
return res.redirect(result.redirectCode, result.redirectView);
}
} else {
sails.log.error('err:', err);
return res.badRequest();
}
});
};
now if i change it to
var moment = require('moment');
function checkLastActivity(req) {
if (!req.session.lastActivity) {
return false; // no session.
} else {
return true;
}
}
function setActivity(req) {
req.session.lastActivity = moment(new Date()).format();
return true;
}
function updateActivity(req, res, updated) {
req.session.lastActivity = moment(new Date()).format();
updated(null, {
isdestroted: 0
});
}
function checkIdleTime(req, idleTime) {
try {
var lastActivityTime = moment(req.session.lastActivity).format();
var currentActivityTime = moment(new Date()).format();
timeIdle = moment.utc(moment(currentActivityTime).diff(moment(lastActivityTime))).format("HH:mm:ss");
minutestimeIdle = moment.duration(timeIdle).asMinutes().toFixed(2);
idleTime(null, minutestimeIdle);
} catch (e) {
idleTime(e, null);
}
}
function destroyActivity(req, res, destroyed) {
try {
req.session.destroy();
sails.log.info("***********************session is expired for user *****************");
destroyed(null, {
isdestroted: 1,
redirectCode: 200,
redirectView: '/login/'
});
} catch (e) {
destroyed(e, null);
}
}
module.exports = function isSession(req, res, next) {
cleanParamsService.cleanPrm(req);
async.waterfall([
function(callback) {
callback(null, checkLastActivity(req));
},
function(isLastActivity, callback) {
if (isLastActivity) {
checkIdleTime(req, function(err, timeIdle) {
if (minutestimeIdle > sails.config.session_timeout) { /*if user is being idle*/
destroyActivity(req, res, function(err, destroyed) {
sails.log.warn('User was idle since' + minutestimeIdle + ', sessions are destroyed');
callback(null, destroyed);
})
} else {
updateActivity(req, res, function(err, updated) {
callback(null, updated);
});
}
})
} else {
updateActivity(req, res, function(err, updated) {
callback(null, updated);
});
}
},
function(resp, callback) {
if (resp.isdestroted == 0) {
req.fresh;
req.session.isdestroted = false;
callback(null, resp);
} else {
req.fresh;
req.session.isdestroted = true;
callback(null, resp);
}
}
], function(err, result) {
if (result) {
if (result.isdestroted == 0) {
return next();
} else {
return res.redirect(result.redirectCode, result.redirectView);
}
} else {
sails.log.error('err:', err);
return res.badRequest();
}
});
};
The application trigger error where i'm setting req.session.isdestroted = true/false and the error is
warn: User was idle since0.38, sessions are destroyed
debug: Lowering sails...
C:\Users\Vbase002\Desktop\CC\Website\api\policies\checkSession.js:83
req.session.isdestroted = true;
^
TypeError: Cannot set property 'isdestroted' of undefined
at C:\Users\Vbase002\Desktop\CC\Website\api\policies\checkSession.js:83:41
at fn (C:\Users\Vbase002\Desktop\CC\Website\node_modules\sails\node_modules\
async\lib\async.js:579:34)
at Immediate._onImmediate (C:\Users\Vbase002\Desktop\CC\Website\node_modules
\sails\node_modules\async\lib\async.js:495:34)
at processImmediate [as _immediateCallback] (timers.js:367:17)
i'm setting this session to show some message on login page to user, but i don't know why sails is behaving like this.
is there any way to handle this scenario and also please let me know the reason why this is happening.
Thanks
This is a behavior from express.
Session.destroy()
Destroys the session, removing req.session, will be re-generated next
request.
As your warn log indicates, req.session.destroy(); has been called before the error.
You could use req.session.regenerate() to have the expected behavior.
Session.regenerate()
To regenerate the session simply invoke the method, once complete a
new SID and Session instance will be initialized at req.session.
Related
I am working on nodejs and i am using "Express js" framework, I am working on Controller and i am trying to get data from "two model functions" but i am getting message "anonymous", How can i get data from multiple "model functions", Here is my code,
This is my homeController
homeController.index = function (req, res, next) {
// fetching data into 'data variable' from FetchRecords model
homeModel.FetchRecords(function (err, data) {
if (err) {
req.flash('error', 'There was no records.');
} else {
res.render('home/index', { data: data });
}
});
homeModel.getAverage(function (average) {
console.log(average);
// res.render('home/index',{data:average});
});
};
This is my homeMOdel
homeModel.FetchRecords = function (data) {
sql.query('SELECT * FROM rating', function (err, res) {
if (err) {
return data(err, null);
} else {
return data(null, res);
}
});
};
homeModel.getAverage = function (average) {
console.log(average);
sql.query('SELECT avg(value) FROM rating', function (err, res) {
if (err) {
return average(err, null);
} else {
return average(null, res);
}
});
};
Inside homeModel just create 1 function instead of 2 separate. You can combine both MySQL queries into one like this.
const FetchRecordsAndAverage = function (data) {
sql.query('SELECT * FROM rating; SELECT avg(value) FROM rating', function (err, res) {
if (err) {
return data(err, null);
} else {
return data(null, res);
}
});
};
module.exports = {
FetchRecordsAndAverage
}
With this you will get combined data of both queries as arrays inside array.
Result of queries can be accessed as data[0] & data[1].
You should export the function from the home model:
const FetchRecords = function (data) {
sql.query('SELECT * FROM rating', function (err, res) {
if (err) {
return data(err, null);
} else {
return data(null, res);
}
});
};
const getAverage = function (average) {
console.log(average);
sql.query('SELECT avg(value) FROM rating', function (err, res) {
if (err) {
return average(err, null);
} else {
return average(null, res);
}
});
};
module.exports = {
FetchRecords,
getAverage
}
And retrieve them in your application by calling:
const { FetchRecords, getAverage } = require('./path/to/home_model');
can somebody help me to update some multiple object in
loopback but i don't have any idea on how to do it..
this is what i tried...
Bond.ParseBondQoutesheet = (data, cb) => { //eslint-disable-line
//// now update multiple
for (let i = 0; i <= data.length; i = +i) {
const filter = {
where: { id: data[i].id },
};
Bond.findOne(filter, (err, newdata) => {
if (!err) {
newdata.updateAttributes(data[i], function (err, updated) {
if (!err) {
if (data.length === i) {
console.log('updated success')
cb(null, updated);
}
} else {
console.log('err')
console.log(err)
cb(err, null);
}
})
} else {
cb(err, null);
}
});
}
};
is this correct?
You can run that but because of JavaScript's async nature it will behave unexpectedly
what you can do in order to solve this would be to loop it using recursive method like this
Bond.ParseBondQoutesheet = (data, cb) => { //eslint-disable-line
//// now update multiple
let data = data;
updateAllSync(0);
function updateAllSync(i) {
if (i < data.length) {
const filter = {
where: { id: data[i].id },
};
Bond.findOne(filter, (err, newdata) => {
if (!err) {
newdata.updateAttributes(data[i], function (err, updated) {
if (!err) {
if (data.length === i) {
console.log('updated success')
updateAllSync(i+1);
}
} else {
console.log('err')
console.log(err)
cb(err, null);
}
})
} else {
cb(err, null);
}
});
}else{
cb(null,i); // finished updating all docs sync
}
}
};
i'm new!
I can't cope with the problem!
Did I try adding 'return' or not?
thx!
'GET /edit': async (ctx, next) => {
cache.get('user', function (err, result) {
if (err) {
console.log(err);
return;
}
var user = JSON.parse(result);
if (user == null) {
ctx.render('login.html', {
title: '登录'
});
} else {
ctx.render('edit.html');
}
})
// ctx.redirect('/login');
}
calling id from mongodb with callback function
var GetID = function (nameval, callback) {
console.log(nameval);
console.log("munesh hello");
GenerateID.find({ "id_name": nameval }, {
"id_code": 1,
"id_value": 1, "_id": 0
}, function (err, genvalue) {
if (err) {
console.log('hello');
}
else {
if (genvalue === null) {
callback(err, false);
}
else {
callback(err, true);
}
}
console.log(genvalue);
});
};
and calling above method so we need
so we need id from GenerateID.GetID and do our own work.
var region_id = GenerateID.GetID(name, function (error, result) {
if (error) {
console.log("getting any error");
} else {
console.log(region_id);
if (!result) {
console.log('data is not coming');
} else {
console.log('data is coming');
}
}
});
You have a number of issues. In the first piece of code, you need to pass the actual value when calling the callback.
In the second, you need to set region_id = result.
Ideally you would do this using promises as demonstrated below.
var GetID = function(nameval){
return new Promise((resolve,reject) => {
console.log(nameval);
console.log("munesh hello");
GenerateId.find({ "id_name" : nameval },{"id_code":1 , "id_value":1, "_id":0},
function( err , genvalue ) {
console.log(genvalue);
if (err) {
console.log('hello');
return reject()
}
if (genvalue == null) { return resolve(false); }
return resolve(genValue);
});
});
}
var GetIDPromise = GenerateId.GetID(name);
GetIDPromise.then(
genValue => {
if ( genValue == false ){
console.log('data is not coming');
// handle region id not being available. Perhaps return and show an error
}else{
var region_id = genValue;
// continue execution and use region id.
}
},
error => {
console.log("getting any error");
}
)
I have this code:
async = require('async')
async.auto({
getUserName: function(callback) {
console.log('**In getUserName.**')
callback(null, 'Lem')
},
connectToDb: function(callback) {
console.log('**In connectToDb.**')
var connected = true
if(connected) {
callback(null, connected)
} else {
callback('Error connecting to DB.', null)
}
},
checkIfUserExist: [
'getUserName',
'connectToDb',
function(callback, results) {
console.log('**In checkIfUserExist.**',
JSON.stringify(results))
var userExist = false
if(userExist) {
callback('User exist in DB.')
} else {
setTimeout(
function() {
callback(null, userExist);
},
1000
);
}
}
],
signup: [
'checkIfUserExist',
function(callback, results) {
console.log('**In signup**', JSON.stringify(results))
var userName = results.getUserName
var isDbConnected = results.connectToDb
var userExist = result.checkIfUserExist
if(userName && isDbConnected && !userExist) {
callback(null,
{'status': '200', 'msg': 'Successfully signed up user'})
} else {
callback('Error signing up user.', null)
}
}
]
},
function(error, results) {
console.log('error = ', error)
console.log('results = ', results)
})
Why am I experiencing this error:
**In getUserName.**
**In connectToDb.**
error = function () {
if (fn === null) throw new Error("Callback was already called.");
var callFn = fn;
fn = null;
callFn.apply(this, arguments);
}
results = undefined
**In checkIfUserExist.** undefined
^[[A/home/lem/js/async/asyncAuto.js:30
callback(null, userExist);
^
TypeError: callback is not a function
at Timeout._onTimeout (/home/lem/js/async/asyncAuto.js:30:13)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
There was a breaking change in async's auto method in v2, which you seem to use.
https://github.com/caolan/async/blob/master/CHANGELOG.md#breaking-changes
auto task functions now always take the callback as the last argument. If a task has dependencies, the results object will be passed as the first argument. To migrate old task functions, wrap them with _.flip
An async function, one that expects a callback as its last argument. Here is my working code.
var async = require('async');
async.auto({
getUserName: function(callback) {
console.log('**In getUserName.**')
callback(null, 'Lem')
},
connectToDb: function(callback) {
console.log('**In connectToDb.**')
var connected = true
if(connected) {
callback(null, connected)
} else {
callback('Error connecting to DB.', null)
}
},
checkIfUserExist: [
'getUserName',
'connectToDb',
function(results, callback) {
console.log('**In checkIfUserExist.**',
JSON.stringify(results))
var userExist = false
if(userExist) {
callback('User exist in DB.')
} else {
setTimeout(function() {
callback(null, userExist);
},1000);
}
}
],
signup: [
'checkIfUserExist',
function(results, callback) {
console.log('**In signup**', JSON.stringify(results))
var userName = results.getUserName
var isDbConnected = results.connectToDb
var userExist = results.checkIfUserExist
if(userName && isDbConnected && !userExist) {
callback(null,
{'status': '200', 'msg': 'Successfully signed up user'})
} else {
callback('Error signing up user.', null)
}
}
],
},
function(error, results) {
console.log('error = ', error)
console.log('results = ', results)
})
function(callback, results) should be function(results, callback)
Please check this url for reference.
https://caolan.github.io/async/docs.html#auto