Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling populate() gets TypeError: Cannot read property 'sync' of undefined #89

Open
mblackritter opened this issue Nov 1, 2017 · 5 comments

Comments

@mblackritter
Copy link

I'm just setting up jsonapi-server to mock our JSON:API, but while I try to populate the DB, I get the following error:

/aniola-cotesco-se/node_modules/jsonapi-store-relationaldb/lib/sqlHandler.js:105
      self.baseModel.sync(options).asCallback(cb)
                    ^

TypeError: Cannot read property 'sync' of undefined
    at /aniola-cotesco-se/node_modules/jsonapi-store-relationaldb/lib/sqlHandler.js:105:21
    at /aniola-cotesco-se/node_modules/async/dist/async.js:3853:24
    at replenish (/aniola-cotesco-se/node_modules/async/dist/async.js:946:17)

I kept my resource file very simple and already tried to remove the attributes, but that doesn't make a difference:

jsonApi.define({
  namespace: 'json:api',
  resource: 'settings',
  handlers: dbHandler,
  attributes: {
    title: jsonApi.Joi.string().required()
      .description('The articles title, should be between 8 and 15 words')
      .example('Learning how to use JSON:API'),
    content: jsonApi.Joi.string().required()
      .description('The main body of the article, provided as HTML')
      .example('<p>Paragraph 1. Lovely.</p><hr /><p>The End.</p>'),
    created: jsonApi.Joi.string().regex(/^[12]\d\d\d-[01]\d-[0123]\d$/)
      .description('The date on which the article was created, YYYY-MM-DD')
      .example('2017-05-01'),
    status: jsonApi.Joi.string().default('published')
      .description('The status of the article - draft, ready, published')
      .example('published'),
    views: jsonApi.Joi.number().default(0)
      .description('Number of views for this article')
  }
})

What might it be that I'm not aware of? 🤗

@paparomeo
Copy link
Contributor

Your error is unexpected. I'll try to reproduce it locally to gain more insight.

@2ZZ
Copy link

2ZZ commented Nov 20, 2017

I also have this issue, node version doesn't seem to make a difference

@vnen
Copy link

vnen commented Mar 11, 2018

I bumped into this. Looking at the source I discovered that you can only call populate() after you called jsonApi.start().

@ghost
Copy link

ghost commented Mar 31, 2019

I also have this problem, is there any solution already?

@ghost
Copy link

ghost commented Apr 3, 2019

Thank to vnen I think I figured this out. Kind of stupid:

You cann't populate the database directly after creating the handler.

Call handler.populate after defining the database resource. In my case:

'use strict';

const jsonApi = require('jsonapi-server');

const handler = require('../handlers/handler');

jsonApi.define({
  resource: "userCourseHighscore",
  handlers: handler,
  attributes: {
    userId: jsonApi.Joi.string(),
    courseId: jsonApi.Joi.string(),
    score: jsonApi.Joi.number(),
    createdAt: jsonApi.Joi.date(),
    updatedAt: jsonApi.Joi.date()
  }
});

handler.populate();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants