0

I have a sample notebook app that works with ember-cli's HTTP mocks and also my rails backend using ActiveModelSerializer.

When I hook it to firebase with ember-fire, I am able to register a user (I see it in the dashboard) but when I try to retrieve it by email, I get the following warning:

WARNING: Encountered "0" in payload, but no model was found for model name "0" (resolved model name using ui@serializer:application:.modelNameFromPayloadKey("0"))

then this error:

Error: Assertion Failed: You must include an 'id' for undefined in an object passed to 'push'
    at new Error (native)
    at Error.EmberError (http://localhost:4200/assets/vendor.js:24735:21)
    at assert (http://localhost:4200/assets/vendor.js:14636:13)
    at Object.assert (http://localhost:4200/assets/vendor.js:22037:34)
    at ember$data$lib$system$store$$Service.extend._pushInternalModel (http://localhost:4200/assets/vendor.js:75316:15)
    at ember$data$lib$system$store$$Service.extend.push [as _super] (http://localhost:4200/assets/vendor.js:75302:34)
    at push (http://localhost:4200/assets/vendor.js:94940:38)
    at superWrapper [as push] (http://localhost:4200/assets/vendor.js:30984:22)
    at http://localhost:4200/assets/vendor.js:70210:27
    at Object.Backburner.run (http://localhost:4200/assets/vendor.js:9707:25)

I am querying the store using:

export default Ember.Route.extend({
  actions: {
    login: function() { 
      this.store.query('user', {
        email: this.controller.get('email') 
      }).then((users) => {
        if(users.get('length') === 1) {
          var user = users.objectAt(0); 
          this.controllerFor('application').set('user',user); 
          this.transitionTo('notebooks', user.get('id'));
        }
        else {
          console.log('unexpected query result'); 
        }
     }); 
    }
  } 
});

Digging in, I can see by setting breakpoint at finders.js#157 I am about to

store._adapterRun(function () { var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, null, 'query'); //TODO Optimize records = store.push(payload); });

push the payload. The adapter payload inspects to

adapterPayload: Array[1]
   0: Object 
       email: "test@test.com"
       first_name: "Test"
       id: "-K1oINClDw2ylQLww7-p"
       last_name: "User"

which is my user. So all's good except for the trace. Not sure about that ID but I am new to firebase; maybe it's ok. It matches what I see in my dashboard.

I haven't done anything special with my serializer -- it's vanilla.

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
});

AFAIK I am using the latest & greatest -- here's bower.json

{
  "name": "ui",
  "dependencies": {
    "ember": "2.1.0",
    "ember-cli-shims": "ember-cli/ember-cli-shims#0.0.4",
    "ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
    "ember-data": "2.1.0",
    "ember-load-initializers": "ember-cli/ember-load-initializers#0.1.7",
    "ember-qunit": "0.4.9",
    "ember-qunit-notifications": "0.0.7",
    "ember-resolver": "~0.1.18",
    "jquery": "^2.1.4",
    "loader.js": "ember-cli/loader.js#3.2.1",
    "qunit": "~1.18.0",
    "foundation": "~5.5.3",
    "bootstrap": "~3.3.5",
    "showdown": "~1.3.0",
    "firebase": "^2.1.0"
  }
}

and my dev dependencies in package.json

  "devDependencies": {
    "body-parser": "^1.14.1",
    "broccoli-asset-rev": "^2.1.2",
    "ember-cli": "1.13.8",
    "ember-cli-app-version": "1.0.0",
    "ember-cli-babel": "^5.1.3",
    "ember-cli-dependency-checker": "^1.0.1",
    "ember-cli-htmlbars": "1.0.1",
    "ember-cli-htmlbars-inline-precompile": "^0.3.1",
    "ember-cli-ic-ajax": "0.2.1",
    "ember-cli-inject-live-reload": "^1.3.1",
    "ember-cli-qunit": "^1.0.0",
    "ember-cli-rails-addon": "0.0.12",
    "ember-cli-release": "0.2.3",
    "ember-cli-showdown": "2.5.0",
    "ember-cli-sri": "^1.0.3",
    "ember-cli-uglify": "^1.2.0",
    "ember-data": "2.1.0",
    "ember-disable-proxy-controllers": "^1.0.0",
    "ember-export-application-global": "^1.0.3",
    "emberfire": "1.6.0",
    "express": "^4.13.3",
    "glob": "^4.5.3",
    "morgan": "^1.6.1",
    "nedb": "^1.2.1"
  }

Any pointers/help/guidance would be great! I am also new to ember too, so maybe I am missing the obvious?

David Hersey
  • 1,049
  • 11
  • 13

1 Answers1

0

You need a type key to be returned from your backend, rather than just sending an array. This lets Ember Data know what type of model you're pushing to the store. Your payload should look like this:

users: [
  {
    email: "test@test.com",
    id: "abcdefg"
    (...)
  }
]

instead of this

[
  {
    email: "test@test.com",
    id: "abcdefg"
    (...)
  }
]

Alternatively you can explicitly pass in the type and data:

store.push("user", store.normalize("user", response[0]));

(where response is still the array)

Tom Netzband
  • 1,110
  • 1
  • 6
  • 13
  • I'm using ember-fire, and from what I understand firebase is going to decide what is being returned from the backend. Ember-fire interacts with the store on that end. I hear what you are saying and it makes sense but I'm guessing there's something I need to tell ember-fire to make that work? – David Hersey Oct 31 '15 at 13:49
  • Hm I see. I've never used Ember-Fire or Firebase. Does your application adapter extend from `DS.FirebaseAdapter`? It looks like the ember-fire addon doesn't overwrite an existing application adapter on install, so you might have to go in and edit your application adapter to be `export default DS.FirebaseSerializer.extend();`. – Tom Netzband Oct 31 '15 at 19:07
  • Yes, it does that: export default FirebaseAdapter.extend({ firebase: inject.service(), }); – David Hersey Oct 31 '15 at 21:32