2016-12-16 92 views
0

这是Node.js异步性质的问题吗?我有一段代码,该代码获取通过Memcache的PHP的会议,像这样:节点无法获取会话ID

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

是的,你在异步函数的问题。

您需要将回调添加到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); 
 
     }); 
 
    }); 
 
}