0

I have the following route model hook:

model() {
    return this.store.query('page').then(
        (result) => { return result; },
        (errors) => { return errors; }
    );
}

I am returning a basic error in my API so I can determine the best way to display errors when the API fails while attempting to populate the initial model.

The response is:

{
    "errors": [
      {
         "detail": "Test",
         "source": {
             "pointer": "data/attributes/general"
         }
      }
    ]
}

This is caught and I am left with a model that contains an ErrorClass.

console error

I expect the error above to be accessed by the "general" key, and be able to do the following in a template:

{{#each model.errors.general as |error|}}
    <div class="error msg_animate">{{error.message}}</div>
{{/each}}

But I am not able to access errors in this manner. It seems that my error response isn't being parsed correctly because I can access the errors as such:

{{#each model.errors as |error index|}}
    <div class="error msg_animate">{{error.detail}} - {{error.source.pointer}}</div>
{{/each}}

This maps to the basic ErrorClass shown above and outputs:

Test - data/attributes/general

This is unexpected. What am I missing?

UPDATED INFORMATION

I have the following that works in another route. The difference being that it returns errors after attempting to save a model (rather than directly from the model() hook:

template.hbs

{{#each model.errors as |error|}}
    <div class="error msg_animate">{{error.message}}</div>
{{/each}}

route.js

actions: {
    submit: function(model){
        model.save().then((resp) => {
            this.transitionTo('admin.page-edit', resp.data.attributes.permalink);
        }, function(resp){
            // errors show on template
        });
    }
}

I am thoroughly confused as to the difference. Both responses from my API seem exactly the same in terms of format.

Gurnzbot
  • 3,742
  • 7
  • 36
  • 55
  • if `model.errors` is an array then eaching `model.errors.general` doesn't really make sense, right? – Mark Fox Oct 05 '16 at 18:48
  • How come doing the following won't work either? `{{#if model.errors.general}}
    {{model.errors.general.message}}
    ` {{/if}}` I'd expect this to work based on other things I've read on SO. I could be way off of course.
    – Gurnzbot Oct 05 '16 at 19:17
  • think about it like `model.errors` is a list of errors so you can access the first one with `model.errors[0]` and if the first error had a general key you could access it like `model.errors[0].general` — handlebars each is designed to be used with list-like structures, not map-like structures – Mark Fox Oct 05 '16 at 19:57
  • 2
    @Gurnzbot Inside hbs you can say `{{log 'model.errors - ' model.errors}}` ..that would print object in console. that should explain a lot. – Ember Freak Oct 06 '16 at 05:54
  • @kumkanillam I didn't know this was a thing :) I find that it logs the error object that is exactly what I passed from the JSON API backend. Has a `detail` and `source -> pointer` keys. I would expect to be able to access the errors from my model as described [here](http://stackoverflow.com/a/32059528/1803539). However, that does not work. Is the issue the fact that I am returning an error from my model() hook initially? Should I be using an error substate instead of my initial template? – Gurnzbot Oct 06 '16 at 13:40
  • @Gurnzbot To be frank, i haven't done this before. In your screen shot,i can see `isAdapter:true` if that is set to true then your error responses are not parsed and set in model `errors` hash. so please ensure status code 422 should come. – Ember Freak Oct 06 '16 at 14:00

0 Answers0