使用新控制器尝试Sails,但视图无法正确呈现。 我的控制器看起来像这样:
/**
* 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.'}]
});
}
};
虽然我的观点如下:
<h1>Preguntas</h1>
<ul>
<% _.each(questions, function(question) { %>
<li><%= question.title %></li>
<% }) %>
</ul>
但我收到以下错误:
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)
关于如何使这项工作的任何想法?
答案 0 :(得分:3)
尝试引用问题
return res.view({
"questions": [{title: 'Freddy a presidente?', content: 'Últimamente Freddy se encuentra con más diplomacia.'}]
});
答案 1 :(得分:0)
您的路由如何在config \ routes.js中定义?
我猜你的代码看起来像是:'/':{view:'main',action:'index'} ,但这只是渲染视图而不调用控制器。
尝试类似:'/':{controller:'main',action:'index'}
此致
答案 2 :(得分:0)
SailsJs文档显示了一个没有引号的视图变量示例:
return res.view({
corndogs: [{name: 'Hank the Corndog'}, {name: 'Lenny the Corndog'}]
});
......但这对我不起作用。我不确定为什么。如果我引用变量名称就可以了。
return res.view({
"corndogs": [{name: 'Hank the Corndog'}, {name: 'Lenny the Corndog'}]
});