简体   繁体   中英

I can't pass data in sailsjs to ejs

i'm trying SailsJs but at the moment to pass the Model.count() value from a table with 4 records, in the ejs view returns "undefined"

// SAILS CODE

module.exports = {
create:function(req,res){
    CountOfUsers=User.count().exec(function (error, found){console.log('There are ' + found + ' users');});
    return res.view('privacy', {
        userCount: CountOfUsers
    });
}};    

// EJS CODE

HELLO WORLD! <%= userCount %>

// THE RESULT

HELLO WORLD! undefined

// COMMAND LINE CONSOLE.LOG

There are 4 users

Thank you so much!

It is also possible to write res.locals.userCount = CountOfUsers; and then later just res.view();

The right sintax is:

module.exports = {
  create:function(req,res) {
    User.count().exec(function (error, found){
      console.log('There are ' + found + ' users');

      return res.view('privacy', {
          userCount: found
      });
    });
  }
}; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM