Answer the question
In order to leave comments, you need to log in
How to return the result of a function from a nested function?
Help please, I'm stupid in the evening. How to do the right one return
in this case? Piece of code:
function readSession (id) {
var fs = require ("fs"),
PHPUnserialize = require('php-unserialize');
fs.readFile("/var/www/cookies/sess_" + id, function (err, boof) {
if (err) throw err;
console.log(PHPUnserialize.unserializeSession(boof.toString()));
return PHPUnserialize.unserializeSession(boof.toString())
})
}
app.get('/balance.json', function (req, res) {
res.setHeader("content-type", "application/json; charset=UTF-8");
res.send(JSON.stringify(readSession(req.cookies.PHPSESSID)));
})
console.log
it is displayed normally, but nothing is displayed on the page, because the result is returned readFile
, notreadSession
Answer the question
In order to leave comments, you need to log in
It may be necessary to add a callback to readSession, it will look like this
function readSession (id,callback) {
var fs = require ("fs"),
PHPUnserialize = require('php-unserialize');
fs.readFile("/var/www/cookies/sess_" + id, function (err, boof) {
if (err) throw err;
console.log(PHPUnserialize.unserializeSession(boof.toString()));
callback( PHPUnserialize.unserializeSession(boof.toString()));
})
}
app.get('/balance.json', function (req, res) {
res.setHeader("content-type", "application/json; charset=UTF-8");
readSession(req.cookies.PHPSESSID,function(readSession) {
res.end(JSON.stringify(readSession));
});
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question