从nodejs中的会话对象中检索记录

时间:2016-02-10 08:22:37

标签: node.js session express

这里是nodejs的新用户,我的session中有以下数据,可在网页发布时使用

{"lstDetails":
     [
      {"ID":"FFSDER2da2411cDSs12CGh21",
       "FirstName":"Test",
       "LastName":"Data",
       "DOB":"8/15/1921 12:00:00 AM",
       "AddressLine1":"Test Address Line 1",
       "AddressLine2":"",
       "City":"FakeCity",
       "State":"ST",
       "Zip":"41511",
       "PID":0,
       "EmailID":"SC4239925@FakeEmail.com",
       "Gender":"1",
       "WorkPhone":"",
       "OtherPhone":"5555555555",
       "ICarier":
                  {"ICName":null,
                  "IGName":null,
                  "IGNum":null
                  }
       },
       {"ID":"DS24DASD5da21afd56D4#2!",
       "FirstName":"Test2",
       "LastName":"Data2",
       "DOB":"8/15/1921 12:00:00 AM",
       "AddressLine1":"Test2 Address Line 1",
       "AddressLine2":"",
       "City":"FakeCity2",
       "State":"ST2",
       "Zip":"41511",
       "PID":0,
       "EmailID":"SC4239925@FakeEmail.com",
       "Gender":"1",
       "WorkPhone":"",
       "OtherPhone":"5555555555",
       "ICarier":
                  {"ICName":null,
                  "IGName":null,
                  "IGNum":null
                  }
       }
     ]
}

以上详细信息将存储在req.Session中。如何使用sessionID对象上方搜索特定数据并获取特定记录?我继续通过网络,但遗憾的是没有找到任何有用的信息。

1 个答案:

答案 0 :(得分:2)

您可以使用过滤器或地图搜索数组以查找所需的对象(节点)。

简化示例:

var myArray = [{
  "ID":"FFSDER2da2411cDSs12CGh21",
  "FirstName":"Test",
  "LastName":"Data"
},
{
  "ID":"DS24DASD5da21afd56D4#2!",
  "FirstName":"Test2",
  "LastName":"Data2"
}];

var result = myArray.filter(function(f) { 
  if (f.ID === 'FFSDER2da2411cDSs12CGh21') { 
    return f; 
  } 
});

在具有一个对象的结果数组中生成结果,在ID上进行过滤。您可以通过比较字符串,RegEx等的部分来过滤