Search code examples
keystonejs

Why is keystonejs throwing an unknown list error on startup in this example?


I've made a small project using keystone with two lists: User and Post. The Admin UI gives me no problem, but querying either list throws an error (in this case, for Posts)

ReferenceError: Unknown keystone list "Post"

Checking my mongo database shows that there is a corresponding collection, and I'm able to add and edit as normal from the Admin UI. My code for posts, Post.js is as follows:

var keystone = require("keystone");
var Types = keystone.Field.Types;

var Post = new keystone.List("Post", {
  autokey: { path: "slug", from: "title", unique: true },
  map: { name: "title" },
  defaultSort: "-createdAt"
});

Post.add({
  title: { type: String, required: true },
  state: {
    type: Types.Select,
    options: "draft, published, archived",
    default: "draft"
  },
  author: { type: Types.Relationship, ref: "User" },
  createdAt: { type: Date, default: Date.now },
  publishedAt: Date,
  content: {
    brief: { type: Types.Html, wysiwyg: true, height: 150 },
    extended: { type: Types.Html, wysiwyg: true, height: 400 }
  }
});

Post.defaultColumns = "title, state|20%, author, publishedAt|15%";
Post.register();

This is copy-pasted directly from the keystone documentation

The following code snippet, getPosts.js is where the error happens, on line 2.

var keystone = require("keystone");
var Post = keystone.list("Post");

module.exports = function(req, res) {
  return res.json(Post.model.find({}));
}; 

I don't think I've deviated too far from the documentation, can anyone spot something I'm doing incorrectly with the syntax? I have a repo here if anyone needs to see more to get a better idea-- it's all in the keystone directory.


Solution

  • I figured out the the problem was. When I initialized keystone, I imported the routes before importing the models, which is why they (the models) were never set at runtime, causing keystone to throw the error. If you have a similar issue, make sure to triple check the order in which you import models/routes/etc.