javascript - Error in accessing JSON object in nodejs -
i have variable var sessions={} in file called 'usersessions.js'. maintaining information of each user in sessions={} unique timestamp. have exported variable make available other files.
when access sessions={} in main file 'app.js' works fine.
but when try access same variable file 'sessiongreet.js', gives error.
here how access data :
suppose '2017-04-07t11:55:40.162z' unique timestamp assigned once.
in app.js:
this works fine:
const usersessions=require('./usersessions.js'); sessionid='2017-04-07t11:55:40.162z'; var data=usersessions.sessions[sessionid].context;
in sessiongreet.js:
this gives error:
const usersessions=require('./usersessions.js'); sessionid='2017-04-07t11:55:40.162z'; var data=usersessions.sessions[sessionid].context;
i know usersessions.sessions[sessionid].context exists accessible in app.js file before accessing in file.
here exact error :
typeerror: cannot read property '2017-04-07t11:55:40.162z' of undefined @ initsession (/media/row_hammer/sessiongreet.js:24:33) @ object.run (/media/row_hammer/sessiongreet.js:67:2) @ object.handlepostback (/media/row_hammer/sessiontemp.js:89:19) @ runpostback (/media/row_hammer/app.js:113:15) @ /media/row_hammer/app.js:161:3 @ object.findorcreatesession (/media/row_hammer/usersessions.js:83:4) @ bot.bot.on (/media/row_hammer/app.js:159:15) @ emitthree (events.js:116:13) @ bot.emit (events.js:194:7) @ bot._handleevent (/media/row_hammer/node_modules/messenger-bot/index.js:254:10)
also, in sessiongreet.js:
//even line shows 'undefined' console.log(usersessions.sessions);
why getting error though flow of program correct?
it may app.js , sessiongreet.js in different folders.
'./usersessions.js' means usersessions.js in same folder file issuing require().
if app.js in same folder usersessions.js, sessiongreet.js in different folder, explain issue.
Comments
Post a Comment