Advertisement
manrajsingh84

SailsJS mongodb data fetch problem.

Sep 15th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Problem fetching data from MongoDB from SailsJS
  2.  
  3. // in file     config/connections.js
  4.  
  5. module.exports.connections = {
  6. // there are other irrelevant connections here as well
  7.   mongodbServer: {
  8.     adapter: 'sails-mongo',
  9.     host: 'localhost',
  10.     port: 27017,
  11.      user: 'user1',
  12.      password: 'password',
  13.      database: 'myCollection'
  14.   },
  15. }
  16.  
  17. // in file     config/models.js
  18. module.exports.models = {
  19.    connection: 'mongodbServer',
  20.    schema: false,
  21.    migrate: 'safe'
  22.  
  23. };
  24.  
  25.  
  26. //Here is my model    in     api/models/Accounts.js
  27. module.exports = {
  28.     connection: "mongodbServer",
  29.    
  30.     attributes: {
  31.         accounts: 'string'
  32.     }
  33. };
  34.  
  35. /*
  36. MongoDB myCollection has field called   "accounts"  which just has email accounts. For example
  37.  
  38. {
  39.     accounts: "abc@gmail.com"
  40. }
  41.  
  42. */
  43.  
  44. // I am try to access that list of accounts from my Controller like the following.
  45.  
  46.     index: function (req,res) {
  47.        
  48.         data = {
  49.             "pageTitle": "Home",
  50.            
  51.         }
  52.  
  53.         Accounts.find().limit(5).exec(function(e,accounts){
  54.           console.log(accounts)
  55.         });
  56.        
  57.         return res.view("home/index",data);
  58.     },
  59.  
  60. // My problem is that console.log(accounts) is always empty.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement