Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'match' of undefined at Ember.DefaultResolver.extend.podBasedComponentsInSubdir

I'm getting a really opaque error message (opaque in the sense I have no point of reference for my own source) from console, I'm not entirely sure where to look, I feel it's likely an error in library code but before posting this on github I'll just double check it's not my own fault.

The Problem

The Problem is simple, I'm calling this.store.find('player'), in hopes to get a list of all players, and then display them in some kind of list, but I'm not even getting past the loading part. The data is pulled from the server and looks properly formatted, but something seems to be failing after the route.model method call. And the error message seems to be somewhere in the ember.js library code with nothing pointing back to my own code.

Server Response

The content type is of course application/json, and note the id property is actually _id.

[
  {
    "_id":"55405a5102b4ed623c225e87",
    "alias":"mikeTest",
    "__v":0,
    "scans":[],
    "createdAt":"2015-04-29T04:13:05.223Z"
  }
]

Error message

Note there is part of the stack trace pointing to my source, only Ember source. Which has made this a pain to debug.

Error while processing route: leader Cannot read property 'match' of undefined TypeError: Cannot read property 'match' of undefined
    at Ember.DefaultResolver.extend.podBasedComponentsInSubdir (http://localhost:4200/assets/vendor.js:60138:76)
    at http://localhost:4200/assets/vendor.js:60190:34
    at Array.exports.default.mixin.Mixin.create.find (http://localhost:4200/assets/vendor.js:39572:30)
    at Ember.DefaultResolver.extend.findModuleName (http://localhost:4200/assets/vendor.js:60188:44)
    at resolveOther (http://localhost:4200/assets/vendor.js:60051:37)
    at superWrapper (http://localhost:4200/assets/vendor.js:28141:20)
    at exports.default.EmberObject.default.extend.resolve (http://localhost:4200/assets/vendor.js:15454:35)
    at Object.resolve [as resolver] (http://localhost:4200/assets/vendor.js:15217:23)
    at resolve (http://localhost:4200/assets/vendor.js:12792:29)
    at Object.Registry.resolve (http://localhost:4200/assets/vendor.js:12336:21)

Source

This ember app is very young, so there is very little source at the moment, but this is all the relevant source at the moment.

Routes

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

export default Router.map(function() {
  this.resource('leader');
  this.resource('profile');
  this.route('loading');
});

Leader route

Leader has a template and a controller, but they are basically empty right now.

import Ember from 'ember';

export default Ember.Route.extend({
  model: function () {
    return Ember.RSVP.hash({
      players: this.get('store').find('player')
    });
  },
});

Player Model

import DS from 'ember-data';

export default DS.Model.extend({
  alias: DS.attr('string'),
  createdAt: DS.attr('date'),
  scans: DS.hasMany('scan'),
});

Application Adapter

import DS from 'ember-data';


export default DS.RESTAdapter.extend({
  namespace: ''
});

Application Serialiser

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  primaryKey: function (type) {
    return '_id';
  },

  serializeId: function(id) {
    return id.toString();
  }
});

Versions

I'm not sure if any of the versions here are particularly important

  • ember-cli is 0.2.3
  • ember-data is 1.0.0-beta.16.1
  • ember is 1.11.1

Things I've tried

  • removing properties from the model, in the event the relationships seemed to be the problem (nothing changed)
  • tried setting up a serialiser and adapter for the application (included above), nothing changed.
    • the serialiser in the event that the id field in the response is actually _id.
  • tried updating ember data, nothing changed.
like image 400
akst Avatar asked Apr 29 '15 07:04

akst


1 Answers

Okay I figured out what was being done wrong... I forgot to check if the data being returned by the server abides to the convention/protocol required to use ember data. The JSON returned by the server looks like this.

[
  {
    "_id":"55405a5102b4ed623c225e87",
    "alias":"mikeTest",
    "__v":0,
    "scans":[],
    "createdAt":"2015-04-29T04:13:05.223Z"
  }
]

It should actually look like this

{
  "players": [
    {
      "_id":"55405a5102b4ed623c225e87",
      "alias":"mikeTest",
      "__v":0,
      "scans":[],
      "createdAt":"2015-04-29T04:13:05.223Z"
    }
  ]
}

So yes this was me being dumb and missing something.

Why is this Required

Ember data expects JSON returned from the server to meet the JSON API Standard, which is a standard that specifies the formatting of the JSON returned from a server. In this case, the data didn't meet the JSON API standard, as I forgot to put the array of players under a key called players. There are some more examples of this in the Ember v1.10.0 guide to models.

The reason Ember Data expects this is so Ember Data can make the certain assumptions about the data returned from the server.

like image 186
akst Avatar answered Sep 30 '22 04:09

akst