这是Node.js异步性质的问题吗?我有一段代码通过Memcache获取PHP会话,如下所示:
var SessionID, Session;
IO.sockets.on('connection', function (client) {
SessionID = client.handshake.headers.cookie;
Session = GetSession(SessionID);
console.log(Session); // here, Session is all the undefined values
});
然后GetSession()
函数如下所示:
function GetSession(SessionID) {
var Session = [];
// Define a default value for all Session values so undefined warnings aren't thrown for non-members
Session = [];
Session['MemberID'] = 0;
Session['MemberEmail'] = undefined;
Session['MemberPass'] = undefined;
// Make sure SessionID has been stripped down to just PHP Session ID
if ( SessionID.split(';').length > 1 ) {
var parts = SessionID.split(';');
var vals = [];
var thisPart, subParts;
for(var k = 0; k < parts.length; k++) {
thisPart = parts[k];
if ( thisPart.indexOf('PHPSESSID') >= 0 ) {
subParts = thisPart.split('=');
if ( subParts.length > 1 )
SessionID = subParts[1];
}
}
}
// Get session vars from Memcache
MemCache.get('Session-'+SessionID, function(err,result){
Session = JSON.parse(result);
console.log(Session); // here, Session prints the correct values
});
return Session;
}
辩论将我依赖Session
的所有代码扔到传递给GetSession()
函数的回调函数中,但这似乎不必要地混乱。
答案 0 :(得分:0)
是的,你在异步函数中遇到问题。
您需要向GetSession添加回调。
var SessionID, Session;
IO.sockets.on('connection', function (client) {
SessionID = client.handshake.headers.cookie;
Session = GetSession(SessionID, function (error, result) {
console.log(result); // here, Session
});
});
function GetSession(SessionID, cb) {
var Session = [];
// Define a default value for all Session values so undefined warnings aren't thrown for non-members
Session = [];
Session['MemberID'] = 0;
Session['MemberEmail'] = undefined;
Session['MemberPass'] = undefined;
// Make sure SessionID has been stripped down to just PHP Session ID
if ( SessionID.split(';').length > 1 ) {
var parts = SessionID.split(';');
var vals = [];
var thisPart, subParts;
for(var k = 0; k < parts.length; k++) {
thisPart = parts[k];
if ( thisPart.indexOf('PHPSESSID') >= 0 ) {
subParts = thisPart.split('=');
if ( subParts.length > 1 )
SessionID = subParts[1];
}
}
}
// Get session vars from Memcache
MemCache.get('Session-'+SessionID, function(err,result){
if(err){
return cb(err);
}
Session = JSON.parse(result);
console.log(Session);
return cb(null, Session);//First arg for Error
});
}
或承诺承诺
var SessionID, Session;
IO.sockets.on('connection', function (client) {
SessionID = client.handshake.headers.cookie;
GetSession(SessionID)
.then(function (result) {
Session = result;
console.log(result); // here, Session is all the undefined values
})
.catch(function (error) {
console.log(error);
});
});
function GetSession(SessionID) {
return new Promise(function (resolve, reject) {
var Session = [];
// Define a default value for all Session values so undefined warnings aren't thrown for non-members
Session = [];
Session['MemberID'] = 0;
Session['MemberEmail'] = undefined;
Session['MemberPass'] = undefined;
// Make sure SessionID has been stripped down to just PHP Session ID
if (SessionID.split(';').length > 1) {
var parts = SessionID.split(';');
var vals = [];
var thisPart, subParts;
for (var k = 0; k < parts.length; k++) {
thisPart = parts[k];
if (thisPart.indexOf('PHPSESSID') >= 0) {
subParts = thisPart.split('=');
if (subParts.length > 1)
SessionID = subParts[1];
}
}
}
// Get session vars from Memcache
MemCache.get('Session-' + SessionID, function (err, result) {
if(err){
return reject(error);
}
Session = JSON.parse(result);
console.log(Session); // here, Session prints the correct values
return resolve(Session);
});
});
}
&#13;