ember.jspromiseember-dataember-model

Emberjs findRecord then not waiting for resolve


I have a findRecord method with the json api convention that looks like this:

this.store.findRecord('book', 2).then((book) =>{
    console.log(book.get('name'));
});

The console log is always printing undefined, but the name value is actually beeing send. book only returns the id and nothing more. I tested with Ember.$ ajax and I got everything. The model structure is also correct.

Is this the normal behavior? This is driving me mad for hours... Thanks.

EDIT:

Working snippet:

Ember.$.getJSON('http://api.xxxxx.com/v1/books/' + id).then((book) => {
    console.log(book.get('name'));
});

EDIT2:

Book model:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';

export default Model.extend({
    name: attr('string'),
    year: attr('number'),
    authors: hasMany('author')
});

Solution

  • If { reload: true } is passed or adapter.shouldReloadRecord evaluates to true, then the returned promise resolves once the adapter returns data, regardless if the requested record is already in the store:

    this.store.findRecord('book', 2, {reload: true}).then((book) =>{
        console.log(book.get('name'));
    });
    

    Refer: https://emberjs.com/api/ember-data/2.16/classes/DS.Store/methods/findRecord?anchor=findRecord