Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js - Controller is rendering view but the collection is undefined

Trying out Sails with a new controller, but the view doesn't render correctly. My controller looks like this:

/**
 * QuestionController
 *
 * @module      :: Controller
 * @description :: Contains logic for handling requests.
 */

module.exports = {

  /* e.g.
  sayHello: function (req, res) {
    res.send('hello world!');
  }
  */
  index: function (req, res) {
    return res.view({
      questions: [{title: 'Freddy a presidente?', content: 'Últimamente Freddy se encuentra con más diplomacia.'}]
    });
  }


};

While my view looks like this:

<h1>Preguntas</h1>
<ul>
  <% _.each(questions, function(question) { %>
  <li><%= question.title %></li>
  <% }) %>
</ul>

But I'm getting the following error:

ReferenceError: /home/alainus/dev/parlamenta/parlamenta/views/question/index.ejs:3
    1| <h1>Preguntas</h1>
    2| <ul>
 >> 3|   <% _.each(questions, function(question) { %>
    4|   <li><%= question.title %></li>
    5|   <% }) %>
    6| </ul>

questions is not defined
    at eval (eval at <anonymous> (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:236:14), <anonymous>:29:68)
    at eval (eval at <anonymous> (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:236:14), <anonymous>:29:228)
    at /home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:249:15
    at Object.exports.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:287:13)
    at Object.exports.renderFile (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs/lib/ejs.js:317:20)
    at View.module.exports [as engine] (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/ejs-locals/index.js:85:7)
    at View.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/express/lib/application.js:506:10)
    at ServerResponse.res.render (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/node_modules/express/lib/response.js:756:7)
    at renderView (/home/alainus/dev/parlamenta/parlamenta/node_modules/sails/lib/hooks/views/index.js:298:20)
    at /home/alainus/dev/parlamenta/parlamenta/node_modules/sails/lib/hooks/views/index.js:280:20
    at Object.oncomplete (fs.js:107:15)

Any ideas on how to make this work?

like image 627
Alain Jacomet Forte Avatar asked Oct 22 '22 02:10

Alain Jacomet Forte


1 Answers

try putting quotes around questions

return res.view({
  "questions": [{title: 'Freddy a presidente?', content: 'Últimamente Freddy se encuentra con más diplomacia.'}]
});
like image 145
Harish Reddy Avatar answered Oct 27 '22 09:10

Harish Reddy