2
votes

I have defined a model(app/models/job.js)

import DS from 'ember-data';

export default DS.Model.extend({
    status: DS.attr(),
    result: DS.attr()
});

And I am trying to load it from the index controller(app/controllers/index.js)

import Ember from 'ember';

export default Ember.Controller.extend({
    productName: "",
    customerName: "",
    startDate: "",
    endDate: "",
    actions: {
        search: function() {
            let data = this.store.find("job", '9e5ce869-89b3-4bfc-a70f-034593c21eae');
            return data;
        }
    }
});

The HTTP response I get is:

{
    "status": "OK",
    "result": {
        "b": 2,
        "a": 2,
        "see": 1,
        "c": 1
    }
}

How ever I get following error and warning:

WARNING: Encountered "status" in payload, but no model was found for model name "status" (resolved model name using next-gen-analytics@serializer:job:.modelNameFromPayloadKey("status"))
WARNING: Encountered "result" in payload, but no model was found for model name "result" (resolved model name using next-gen-analytics@serializer:job:.modelNameFromPayloadKey("result"))
TypeError: Cannot read property '_internalModel' of undefined
at finders.js:50
at Object.Backburner.run (ember.debug.js:224)
at ember$data$lib$system$store$$Service.extend._adapterRun (store.js:2043)
at finders.js:45
at tryCatch (ember.debug.js:56151)
at invokeCallback (ember.debug.js:56166)
at publish (ember.debug.js:56134)
at ember.debug.js:32577
at Queue.invoke (ember.debug.js:910)
at Object.Queue.flush (ember.debug.js:974)onerrorDefault @ ember.debug.js:32616exports.default.trigger @ ember.debug.js:56792Promise._onerror @ ember.debug.js:57758publishRejection @ ember.debug.js:56065(anonymous function) @ ember.debug.js:32577Queue.invoke @ ember.debug.js:910Queue.flush @ ember.debug.js:974DeferredActionQueues.flush @ ember.debug.js:770Backburner.end @ ember.debug.js:160Backburner.run @ ember.debug.js:228run @ ember.debug.js:20238ember$data$lib$system$adapter$$default.extend.ajax.Ember.RSVP.Promise.hash.success @ rest-adapter.js:831jQuery.Callbacks.fire @ jquery.js:3148jQuery.Callbacks.self.fireWith @ jquery.js:3260done @ jquery.js:9314jQuery.ajaxTransport.send.callback @ jquery.js:9718

Any suggestion will be appreciated

UPDATE I was thinking this is kind of a bug, so I went ahead to log a bug on ember-data github repo, got the response from @wecc in an hour or so (NICE) https://github.com/emberjs/data/issues/3683

So to fix this issue, I wrote my own serializer.

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
normalizePayload: function(payload) {
    return {
        'job': {
            id: '9e5ce869-89b3-4bfc-a70f-034593c21eae',
            status: payload.status,
            result: payload.result
        }
    };

}
});

And now, it starts working.

OPINION I can think of why they implemented the default RESTSerializer this way, but we probably should give more information to the users on the documentation of the ember-data, otherwise, newbie who is trying to use it will get lost.

2

2 Answers

5
votes

1) Your server response should have a root element with the same name, as model's. So, for single object it shuld be:

{
    "job": {
        "id": "<backend must provide an id, integer preferred>"
        "status": "OK",
        "result": {
            "b": 2,
            "a": 2,
            "see": 1,
            "c": 1
        }
}

And for multiple objects:

{
    "jobs": [{
        "id": "<backend must provide an id, integer preferred>"
        "status": "OK",
        "result": {
            "b": 2,
            "a": 2,
            "see": 1,
            "c": 1
        }, {/*next object*/}]
}
0
votes

I tried to debug it, and I found that the code goes to rest-serializer.js and extractSingle function, and goes to the line "if (!store.modelFactoryFor(modelName)) {" and it returns "false".

extractSingle: function (store, primaryTypeClass, rawPayload, recordId) {
Ember.deprecate("`serializer.normalizePayload` has been deprecated. Please use `serializer.normalizeResponse` with the new Serializer API to modify the payload.", this.normalizePayload === JSONSerializer.prototype.normalizePayload, {
  id: "ds.serializer.normalize-payload-deprecated",
  until: "2.0.0"
});
var payload = this.normalizePayload(rawPayload);
var primaryRecord;

for (var prop in payload) {
  var modelName = this.modelNameFromPayloadKey(prop);

  if (!store.modelFactoryFor(modelName)) {
    Ember.warn(this.warnMessageNoModelForKey(prop, modelName), false, {
      id: "ds.serializer.model-for-key-missing"
    });
    continue;
  }
  var isPrimary = this.isPrimaryType(store, modelName, primaryTypeClass);
  var value = payload[prop];