【问题标题】:Cannot read property 'on' of undefined : Error in Keystone.js无法读取未定义的“on”属性:Keystone.js 中的错误
【发布时间】:2017-12-22 12:45:07
【问题描述】:

我一直在关注https://leanpub.com/keystonejs/read 的教程并尝试建立一个基本网站。

为此,想为/ 构建一条路线,其中包含一个视图文件index.js 和`index.twig' 中的模板

以下是目录结构

routes/
|-- index.js
|-- middleware.js
`-- views
    |-- blog.js
    |-- contact.js
    |-- gallery.js
    |-- index.js
    `-- post.js
templates/
|-- layouts
|   `-- default.twig
|-- mixins
|   `-- flash-messages.twig
`-- views
    |-- assets -> public/assets/
    |-- blog.twig
    |-- contact.twig
    |-- errors
    |   |-- 404.twig
    |   `-- 500.twig
    |-- gallery.twig
    |-- index.html -> templates/views/index.twig
    |-- index.twig
    |-- post.twig
models/
|-- Audience.js
|-- Element.js
|-- Enquiry.js
|-- Gallery.js
|-- Guide.js
|-- Post.js
|-- PostCategory.js
|-- Section.js
|-- Subsection.js
`-- User.js

以下是我的keystone.jsroutes/index.jsroutes/views/index.js 文件。

    // Simulate config options from your production environment by
// customising the .env file in your project's root folder.
require('dotenv').config();

// Require keystone
var keystone = require('keystone');
var Twig = require('twig');

// Initialise Keystone with your project's configuration.
// See http://keystonejs.com/guide/config for available options
// and documentation.

keystone.init({
    'name': 'Bodhini',
    'brand': 'Bodhini',

    'less': 'public',
    'static': 'public',
    'favicon': 'public/favicon.ico',
    'views': 'templates/views',
    'view engine': 'twig',

    'twig options': { method: 'fs' },
    'custom engine': Twig.render,

    'auto update': true,
    'session': true,
    'auth': true,
    'user model': 'User',
});

// Load your project's Models
keystone.import('models');

// Setup common locals for your templates. The following are required for the
// bundled templates and layouts. Any runtime locals (that should be set uniquely
// for each request) should be added to ./routes/middleware.js
keystone.set('locals', {
    _: require('lodash'),
    env: keystone.get('env'),
    utils: keystone.utils,
    editable: keystone.content.editable,
});

    // Load your project's Routes
    keystone.set('routes', require('./routes'));


    // Configure the navigation bar in Keystone's Admin UI
    keystone.set('nav', {
    posts: ['posts', 'post-categories'],
    galleries: 'galleries',
    enquiries: 'enquiries',
    users: 'users',
    Guide: ['guides','sections','subsections','audiences'],
    element:'elements',});
    // Start Keystone to connect to your database and initialise the web server

routes/index.js

var keystone = require('keystone');
var middleware = require('./middleware');
var importRoutes = keystone.importer(__dirname);

// Common Middleware
keystone.pre('routes', middleware.initLocals);
keystone.pre('render', middleware.flashMessages);

// Import Route Controllers
var routes = {
    views: importRoutes('./views'),
};

// Setup Route Bindings
exports = module.exports = function (app) {
    // Views
    app.get('/', routes.views.index);
    app.get('/blog/:category?', routes.views.blog);
    app.get('/blog/post/:post', routes.views.post);
    app.get('/gallery', routes.views.gallery);
    app.all('/contact', routes.views.contact);

    // NOTE: To protect a route so that only admins can see it, use the requireUser middleware:
    // app.get('/protected', middleware.requireUser, routes.views.protected);

};

路由/视图/index.js

var keystone = require('keystone');

exports = module.exports = function(req, res){
    var view = keystone.View(req, res);
    var locals = res.locals;

    locals.data={
        story:{},
        work:{},
        why:{},
        disclaimer:{},
    };


    view.on('init', function(next){
    var q = kestone.list('Element').model.findOne({slug: 'story'});
        q.exec(function(err, result){
        locals.data.story = result;
            next(err);

        });

    });

    view.render('index');

当我运行 node keystone 然后尝试在浏览器中打开路径 / 时,我得到一个 404 错误以及此输出。

------------------------------------------------
KeystoneJS Started:
Bodhini is ready on http://0.0.0.0:3000
------------------------------------------------

Error thrown for request: /
TypeError: Cannot read property 'on' of undefined
    at module.exports (/home/auditor/Sites/Bodhini/routes/views/index.js:35:9)
    at Layer.handle [as handle_request] (/home/auditor/Sites/Bodhini/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/auditor/Sites/Bodhini/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/home/auditor/Sites/Bodhini/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/auditor/Sites/Bodhini/node_modules/express/lib/router/layer.js:95:5)
    at /home/auditor/Sites/Bodhini/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/home/auditor/Sites/Bodhini/node_modules/express/lib/router/index.js:330:12)
    at next (/home/auditor/Sites/Bodhini/node_modules/express/lib/router/index.js:271:10)
    at /home/auditor/Sites/Bodhini/node_modules/grappling-hook/index.js:198:10
    at _combinedTickCallback (internal/process/next_tick.js:95:7)
    at process._tickCallback (internal/process/next_tick.js:161:9)
GET / 500 16.644 ms

请指导我如何解决此问题。我是 node.js 和 express/keystone 的新手

【问题讨论】:

    标签: node.js express keystonejs


    【解决方案1】:
    var view = keystone.View(req, res);
    

    需要

    var view = new keystone.View(req, res);
    

    因为它是一个构造函数。 Keystone Docs

    【讨论】:

    • @BMC 如果您觉得有用,请务必接受答案,这样你们俩都可以获得一点声望
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2018-12-17
    • 2016-06-30
    • 2016-05-09
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多