Cannot read property 'create' of undefined


#1

I am attempting to create an Ember application that will act as a front end that fetches data from a WordPress application using the add on ember-wordpress.

I have installed the WP Menu API plugin within WordPress that gives me an end point I can query to fetch menu data in WordPress.

I have created a model for my menu

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  count: DS.attr('number'),
  items: DS.hasMany('item')
});

And an item model

import DS from 'ember-data';

export default DS.Model.extend({
  title: DS.attr('string')
});

And here is my route.

import Ember from ‘ember’;

export default Ember.Route.extend({

  model() {
    return  this.store.findRecord('menu', 2);
    }


});

Here is the JSON that my API is bringing back.

https://pastebin.com/15Q3kh0B

I get the error in my console:

Cannot read property ‘create’ of undefined

When I try loop over the items. It works fine when I remove the items model and I can access the menu name etc. I want to be able to store the items in an array and then loop over them in my template.


#2

What do you mean you get the error when you try to loop over the items? How are you trying to loop over them and where ( controller? a component? etc ).

In order to loop the items in your template:

  1. Add a belongsTo relationship in the item model: menu: belongsTo(‘menu’)

  2. In your template, just do

    each model.items as |item| …


#3

You need to inject ‘store’ service in your route

import Ember from ‘ember’;
export default Ember.Route.extend({
  store: Ember.inject.service(),
  model() {
    var store = this.get('store');
    return store.findRecord('menu', 2);
    }
});