无法读取未定义的属性'on':Keystone.js中的错误

时间:2017-07-17 20:46:25

标签: node.js express keystonejs

我一直关注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

路由/ 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

的新手

1 个答案:

答案 0 :(得分:2)

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

需要

{{1}}

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