The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

ryanthtra (5) [Avatar] Offline
#1
Should be:

res.render('users/index', {users: res.local.users, flashMessages: {success: "Loaded all users!"}});


(Replace the value of the users field with res.local.users.)
TheWexler (62) [Avatar] Offline
#2
Thanks for the comment! In this example, we're assuming the variable 'users' is defined earlier before we return the response. Your code 'res.locals.users' assumes we already defined a local variable on the response by the name 'users' and assigned that variable to the list of users. In that case, we don't need to pass any new information from the response into the view, as it already exists in res.locals.users. In other words:

Assuming we queried the database for users and the results came back in a parameter called 'users' we can pass a local variable and assign it to the results with this code -->

res.render('users/index', {users: users, flashMessages: {success: "Loaded all users!"}});


or we can set up a local variable on the response object then render our page in a separate statement, like so -->

res.locals.users = users;
res.render('users/index', {flashMessages: {success: "Loaded all users!"}});