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

问题描述:

我一直在关注教程https://leanpub.com/keystonejs/read并尝试设置基本网站。无法读取属性'on'未定义:Keystone.js中的错误

对于本想建立一个路由/以查看文件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和快递/梯形

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

需求是

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

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

+0

谢谢。修复它。 – BMC

+1

@BMC请务必接受答案,如果你觉得有用,所以你们都可以获得一个小代表 – scniro