Handling session variables in view file in sails.js

Nakarmi

I am trying to use a session variable in my view file. My controller code is like this

User.find()
.where({ username: username })
.where({ password: password })
.exec(function(err, users) {
  if(users.length == 1){
    req.session.user_detail = users; 
    res.redirect('/member');
  }else{
    console.log(err);  
  }
});

I want to use the req.session.user_detail in my member.ejs file.

sgress454

For the record, in case someone stumbles onto this question instead of this one, req.session is indeed available by default in your Sails views. So you can just do

<%= JSON.stringify(req.session.user_detail) %>

in your view; there's no need to pass it explicitly in res.locals. You can also loop through the users with:

<%  req.session.user_detail.forEach(function(user_detail) { %>
<%=   JSON.stringify(user_detail) %>
<%  }) %>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Handling session/cookie with Sails.JS and AngularJS

From Dev

In sails.js, how to access session variables outside controller?

From Dev

In sails.js, how to access session variables in method beforeCreate of model?

From Dev

Handling variables and functions declared on external js file

From Dev

Handling exceptions in Sails.js

From Dev

Load a javascript/coffeescript file only for one view in Sails.js

From Dev

Load a javascript/coffeescript file only for one view in Sails.js

From Dev

sails.js session variables getting lost when API is accessed from android

From Dev

Handling database environment configuration in Sails.js

From Dev

Handling uploads with skipper in sails.js (on progress)

From Dev

Prevent Sails.js from handling errors

From Dev

sails.js Use session param in model

From Dev

Persistent Session with passportjs + sails.js Implementation

From Dev

Accessing the session on socket requests with sails.js

From Dev

sails.js Use session param in model

From Dev

Sails.js session undefined on page refresh

From Dev

Sails js Error in file Upload

From Dev

Accessing session variables in sailsjs view

From Dev

Create config variables in sails.js?

From Dev

Send sails.js variables to Javascript files

From Dev

Sails js :: Testing variables passed to views

From Dev

Sails Js view engine with layout sections support?

From Dev

Sails.js: load script on a specific view

From Dev

Custom view/action/controller not working in Sails JS

From Dev

Including view specific assets in Sails.js

From Dev

Sails.js - passing data to a view

From Dev

Sails Js view engine with layout sections support?

From Dev

Sails.js: load script on a specific view

From Dev

sails js layout view change folder

Related Related

  1. 1

    Handling session/cookie with Sails.JS and AngularJS

  2. 2

    In sails.js, how to access session variables outside controller?

  3. 3

    In sails.js, how to access session variables in method beforeCreate of model?

  4. 4

    Handling variables and functions declared on external js file

  5. 5

    Handling exceptions in Sails.js

  6. 6

    Load a javascript/coffeescript file only for one view in Sails.js

  7. 7

    Load a javascript/coffeescript file only for one view in Sails.js

  8. 8

    sails.js session variables getting lost when API is accessed from android

  9. 9

    Handling database environment configuration in Sails.js

  10. 10

    Handling uploads with skipper in sails.js (on progress)

  11. 11

    Prevent Sails.js from handling errors

  12. 12

    sails.js Use session param in model

  13. 13

    Persistent Session with passportjs + sails.js Implementation

  14. 14

    Accessing the session on socket requests with sails.js

  15. 15

    sails.js Use session param in model

  16. 16

    Sails.js session undefined on page refresh

  17. 17

    Sails js Error in file Upload

  18. 18

    Accessing session variables in sailsjs view

  19. 19

    Create config variables in sails.js?

  20. 20

    Send sails.js variables to Javascript files

  21. 21

    Sails js :: Testing variables passed to views

  22. 22

    Sails Js view engine with layout sections support?

  23. 23

    Sails.js: load script on a specific view

  24. 24

    Custom view/action/controller not working in Sails JS

  25. 25

    Including view specific assets in Sails.js

  26. 26

    Sails.js - passing data to a view

  27. 27

    Sails Js view engine with layout sections support?

  28. 28

    Sails.js: load script on a specific view

  29. 29

    sails js layout view change folder

HotTag

Archive