BMC
BMC

Reputation: 601

Cannot read property 'on' of undefined : Error in Keystone.js

I have been following the tutorial at https://leanpub.com/keystonejs/read and trying to set up a basic website.

For this wanted to build a route for / with a view file index.js and template at `index.twig'

The following is the directory structure

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

Following are my keystone.js, routes/index.js and routes/views/index.js files.

    // 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);

};

routes/views/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');

While I run node keystone and then try opening path / in browser, I get a 404 error along with this output.

------------------------------------------------
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

Please guide me on how to fix this. I am new to node.js and express/keystone

Upvotes: 0

Views: 1402

Answers (1)

Joe
Joe

Reputation: 82554

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

needs to be

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

since it is a constructor. Keystone Docs

Upvotes: 3

Related Questions