我在我的node.js项目中使用sails 我有res.view()的问题,有时当视图控制器动作应用程序退出并在控制台中显示这一行时:
/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:77
throw err; // Rethrow non-MySQL errors
^
TypeError: res.view is not a function
at findCB (/var/www/nodejs/khayyam_computer/api/controllers/EventsController.js:119:29)
at wrapper (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/lodash/index.js:3592:19)
at applyInOriginalCtx (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
at wrappedCallback (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:320:18)
at callback.success (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/normalize.js:33:31)
at _switch (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/factory.js:58:28)
at returnResults (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:180:9)
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:86:16
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:83:7
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at Object.async.forEachOf.async.eachOf (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:236:30)
at Object.async.forEach.async.each (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:209:22)
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:436:11
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:574:5
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at Object.async.forEachOf.async.eachOf (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:236:30)
at Object.async.forEach.async.each (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:209:22)
at _buildChildOpts (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:464:9)
at _execChildOpts (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:432:8)
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:81:10
at wrapper (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/lodash/index.js:3592:19)
at applyInOriginalCtx (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
at wrappedCallback (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:320:18)
at callback.success (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/normalize.js:33:31)
at _switch (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/factory.js:58:28)
at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:166:7
at wrapper (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/lodash/index.js:3592:19)
at applyInOriginalCtx (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
Process finished with exit code 1
我不知道为什么!!!
我的行动代码:
view: function (req, res) {
var event_id = req.param("id", ''); // get id event
if (event_id.trim() != null && event_id.trim().length > 0) { // check event id value
Event.findOne({id: event_id, type: 'event', 'status': 'published'}).exec(function findCB(error, event) {
if (error)
return res.badRequest({error: req.__("event id Can't be Null")});
if (typeof event != null && typeof event != "undefined") {
var moment = require('moment-jalaali');
moment.loadPersian();
moment.locale('fa', {
calendar: {
sameElse: "jD jMMMM jYYYY HH:mm"
}
});
if (req.xhr) {
res.json({moment: moment, event: event});
res.end();
} else {
breadcrumb.create(res, req, [
['رویدادها', 'events/'],
[event.title, 'events/' + event.id]
]);
res.view('events/view', {moment: moment, event: event});
res.end();
}
} else
return res.notFound(404, '404');
});
} else {
return res.badRequest({error: {"rule": "id", "message": req.__("event ID required and must be integer")}});
}
}
其他控制器和操作中也存在此问题
答案 0 :(得分:0)
确保您已启用名为 views
的内置船帆钩子。
看看 .sailsrc
文件,它可能看起来像这样:
{
"generators": {
"modules": {}
},
"_generatedWith": {
"sails": "1.4.2",
"sails-generate": "2.0.3"
},
"hooks": {
"pubsub": false,
"blueprints": false,
"session": false,
"views": false,
"security": false,
"i18n": false
}
}
您需要从文件中完全删除这一行,而不是将其切换到 yes
状态。