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.

6
  • if model.errors is an array then eaching model.errors.general doesn't really make sense, right? Commented Oct 5, 2016 at 18:48
  • How come doing the following won't work either? {{#if model.errors.general}} <div class="error msg_animate">{{model.errors.general.message}} </div> {{/if}}` I'd expect this to work based on other things I've read on SO. I could be way off of course. Commented Oct 5, 2016 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 Commented Oct 5, 2016 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. Commented Oct 6, 2016 at 5: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. 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? Commented Oct 6, 2016 at 13:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.