Keystone-classic: “Event.model is not a constructor” Error In Keystone 4.0

Created on 2 Oct 2018  ·  6Comments  ·  Source: keystonejs/keystone-classic


When Post form data getting below errors.

var keystone = require('keystone');
var Event = keystone.List('Event');

module.exports = function (req, res) {
if (!req.body.name || !req.body.startTime || !req.body.endTime) {
return res.send({ status: 'incomplete data set' });
}
var newEvent = new Event.model();
// var newEvent = new Event.model(req.body);

Event.updateItem(newEvent, req.body, function (error) {
res.locals.enquirySubmitted = true;
if (error) res.locals.saveError = true;
res.render('addEvent');
});
};

TypeError: Event.model is not a constructor
at module.exports (/Users/rigalpatel/KS_shopingcart/routes/api/event/post.js:14:18)
at Layer.handle [as handle_request] (/Users/rigalpatel/KS_shopingcart/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/rigalpatel/KS_shopingcart/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/rigalpatel/KS_shopingcart/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/rigalpatel/KS_shopingcart/node_modules/express/lib/router/layer.js:95:5)
at /Users/rigalpatel/KS_shopingcart/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/rigalpatel/KS_shopingcart/node_modules/express/lib/router/index.js:335:12)
at next (/Users/rigalpatel/KS_shopingcart/node_modules/express/lib/router/index.js:275:10)
at /Users/rigalpatel/KS_shopingcart/node_modules/grappling-hook/index.js:198:10
at process._tickCallback (internal/process/next_tick.js:61:11)

Expected behavior


Actual/Current behavior


Steps to reproduce the actual/current behavior


Environment

| Software | Version
| ---------------- | -------
| Keystone | 4.0.0
| Node.js | 10.9.0

question

Most helpful comment

As @chanoch pointed out. There is a typo in the step 4 tutorial.

var keystone = require('keystone');
var Event = keystone.List('Event');  // <---- This line is typo

module.exports = function (req, res) {
  if (!req.body.name || !req.body.startTime || !req.body.endTime) {
    return res.send({ status: 'incomplete data set' });
  }

  var newEvent = new Event.model();
  Event.updateItem(newEvent, req.body);

};

it should be
var Event = keystone.list('Event');

What's more, in keystone.js, I would suggest to put the import models line above the routes, e.g

keystone.import('models');

keystone.set('routes', require('./routes'));

keystone.start();

All 6 comments

Should the keystone.List('Post') your line keystone.list be capitalised? In my code I use keystone.list()

Seeing the same thing when completing step 4 of the keystone js walkthrough. Related to #4744 ?

I am getting the same error after following the Keystone tutorial.

Software | Version
-- | --
Keystone | 4.0.0
Node.js | 11.0.0

Is there some other way to update document objects? Looking at the updateItem() docs I don't see another approach using Keystone. This is a pretty big issue, no?

I've created a GitHub repo that documents this issue in hopes that with better documentation it may be fixed. Here's a link to a GitHub repo for this issue. I will take the repo down when this issue closes.
Because of this issue, as far as I know 🤔, users cannot create new data by submitting post requests as instructed by the Start from Scratch tutorial. Users can still create new data through the configured admin interface 😥, but post requests result in an error. Here it is again, but formatted.

Error thrown for request: /api/event
TypeError: Event.model is not a function
    at module.exports (/Users/dylan/programming/experimenting/keystone-model-issue/routes/api/event/post.js:9:24)
    at Layer.handle [as handle_request] (/Users/dylan/programming/experimenting/keystone-model-issue/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dylan/programming/experimenting/keystone-model-issue/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/dylan/programming/experimenting/keystone-model-issue/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/dylan/programming/experimenting/keystone-model-issue/node_modules/express/lib/router/layer.js:95:5)
    at /Users/dylan/programming/experimenting/keystone-model-issue/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/dylan/programming/experimenting/keystone-model-issue/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/dylan/programming/experimenting/keystone-model-issue/node_modules/express/lib/router/index.js:275:10)
    at /Users/dylan/programming/experimenting/keystone-model-issue/node_modules/grappling-hook/index.js:198:10
    at process._tickCallback (internal/process/next_tick.js:61:11)

I am just trying to be helpful. No gripes, and now relieved after realizing the admin interface still works to create new data. I have been putting off a project under the poor assumption that nothing data related would work because of this issue. Thanks for making Keystone, it's good so far! 🎆

As @chanoch pointed out. There is a typo in the step 4 tutorial.

var keystone = require('keystone');
var Event = keystone.List('Event');  // <---- This line is typo

module.exports = function (req, res) {
  if (!req.body.name || !req.body.startTime || !req.body.endTime) {
    return res.send({ status: 'incomplete data set' });
  }

  var newEvent = new Event.model();
  Event.updateItem(newEvent, req.body);

};

it should be
var Event = keystone.list('Event');

What's more, in keystone.js, I would suggest to put the import models line above the routes, e.g

keystone.import('models');

keystone.set('routes', require('./routes'));

keystone.start();

Ah, I was correcting the typo in the wrong location, within the files where models are declared.
keystone.List(...) is a class constructor for models while `keystone.list(...)' is used to retrieve an already created model.
I've been tripping over the documentation, but less so the more carefully I read. Thanks @chung2014 !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joernroeder picture joernroeder  ·  5Comments

useralive003 picture useralive003  ·  5Comments

koenoe picture koenoe  ·  4Comments

zhdan88vadim picture zhdan88vadim  ·  5Comments

Twansparant picture Twansparant  ·  5Comments