无法在node.js

时间:2016-02-10 12:23:28

标签: javascript node.js session

我正在为session search data中的ajax分配nodejs分配一组值,如下所示:

req.session.searchData=result.GetSearchDataResult;

当点击任何渲染记录时,用户将被重定向到新view,而在服务器中,我正在加载与该被点击记录相关的不同数据。我正在传递id作为url参数,我计划通过过滤从存储的session.searchData获取记录。但即使这些值存在于session.searchData中,我也无法将其分配给任何变量来读取值和过滤器。

当我在console.log req.session.searchData下面是我得到的:

{"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
                  }
       }
     ]
}

但是当我尝试将其分配给变量时,它总是 undefined

app.use(session({secret: 'secretkey'}));
console.log(req.session.searchData); //this prints above list values
var patientArray = req.session.searchData; 
console.log("Patient Array - " +patientArray); //this prints "Patient Array - undefined"

我也尝试使用app.get如下,但没有运气。

var patientArray;
app.get("/",function(req,res){
  var sess=req.session;
  patientArray=sess.searchData; //never enters here
});
console.log(patientArray);

但从不调用上述方法。

我在这里缺少什么?如何读取会话值并将其分配给变量?

1 个答案:

答案 0 :(得分:0)

由于req.session.searchData=result.GetSearchDataResult;过去常常返回json-response,因此相同的json-response会存储在req.session.searchData中。不确定这是否是正确的行为,但后来我不得不使用JSON.parse(req.session.searchData);来获取正确的数据。