2014-10-28 49 views
0

我試圖SailsJs但此刻從表4層的記錄通過Model.count()值,在EJS視圖返回「未定義」我不能sailsjs數據傳遞到EJS

// 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 %> 

//結果

HELLO WORLD! undefined 

//命令行CONSOLE.LOG

There are 4 users 

太謝謝你了!

回答

1

正確sintax是:

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

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

如果我想傳遞兩個或三個瓦爾?導致查看功能在計數功能回調... – 2014-10-28 22:24:53

+0

node.js和JavaScript的工作是這樣的,你只需調用res.view後去數據庫取數據,所以它是在回調 – 2014-10-29 18:25:35

+0

權利,但如果我想傳遞3或4個不同的變量,如:「頁面標題」,「用戶數組」,「哥倫比亞天氣」...帆如何讓這個?用戶數組的相同回調無法訪問哥倫比亞數據中的天氣... – 2014-10-29 19:10:49

2

也可以寫res.locals.userCount = CountOfUsers;再後來只是res.view();