我正在玩SailsJS,我得到了以下观点:
<p>Products <%= category %></p>
使用以下控制器:
module.exports = {
get: function(req, res) {
res.view('pages/products', {
category: req.params.category
});
}
};
路线:
'/products/:category': 'ProductsController.get',
导航到真正的简单设置时,我得到以下内容:
error: Sending 500 ("Server Error") response:
ReferenceError: /var/www/html/curioushaven/views/pages/products.ejs:1
>> 1| <p>Products <%= category %></p>
2|
category is not defined
at eval (eval at <anonymous> (/usr/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:237:14), <anonymous>:30:54)
at eval (eval at <anonymous> (/usr/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:237:14), <anonymous>:30:80)
at /usr/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:250:15
at Object.exports.render (/usr/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:288:13)
at Object.exports.renderFile (/usr/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:318:20)
at SailsView.module.exports [as engine] (/usr/lib/node_modules/sails/node_modules/ejs-locals/index.js:85:7)
at SailsView.View.render (/usr/lib/node_modules/sails/node_modules/express/lib/view.js:76:8)
at Function.app.render (/usr/lib/node_modules/sails/node_modules/express/lib/application.js:561:10)
at ServerResponse.res.render (/usr/lib/node_modules/sails/node_modules/express/lib/response.js:845:7)
at ServerResponse.res.view (/usr/lib/node_modules/sails/lib/hooks/views/res.view.js:284:16)
at Object.module.exports.get (/var/www/html/curioushaven/api/controllers/ProductsController.js:10:7)
at wrapper (/usr/lib/node_modules/sails/node_modules/lodash/index.js:3095:19)
at routeTargetFnWrapper (/usr/lib/node_modules/sails/lib/router/bind.js:181:5)
at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
at pass (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
at nextRoute (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
at alwaysAllow (/usr/lib/node_modules/sails/lib/hooks/policies/index.js:224:11)
at routeTargetFnWrapper (/usr/lib/node_modules/sails/lib/router/bind.js:181:5)
at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
任何人都可以解释我做错了什么吗?谢谢!
修改 - 我应该指出,我已尝试过各种形式的req.param('category')
和req.params.category
等等......
答案 0 :(得分:0)
而不是使用
req.param('category')
此
req.params.id
应该解决你的问题。但是req.param('category')
应该也可以正常运行,而且从代码中我看不出为什么它不起作用。