HasMany model not returning relations if the previous model has de same relations

Hi, I’m new to Ember but I believe I found a bug or something. Let me tell you in details what happens in the app I’m currently developing.

Twiddle: Ember Twiddle

This is my route model:

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

export default Model.extend({
    name: attr('string'),
    category: attr('string'),
    image: attr('string'),
    creator: belongsTo('user'),
    pois: hasMany('poi')
});

This is my poi model:

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

export default Model.extend({
    name: attr('string'),
    address: attr('string'),
    lat: attr('number'),
    lon: attr('number'),
    rating: attr('number'),
    type: attr('number'),
    owner: belongsTo('user'),
    inroutes: attr('number'),
    title: attr('string'),
    text: attr('string'),
    route: belongsTo('route'),
    images: hasMany('image'),
    primary_image: Ember.computed('images', function(){
        return this.get('images').get('firstObject');
    })
});

Then I have a router called route.js:

import Ember from 'ember';

export default Ember.Route.extend({
    model(params){
        return {route_id: params.route_id};
    },
    actions: {
        didTransition(){
            var $this = this;
            var $model = $this.get('currentModel');
            var $roteiro = {};
            let Controller = Ember.getOwner(this).lookup('controller:lang');
            
            this.store.findRecord('route', $model.route_id).then((route) => {
                $roteiro = {
                    id: route.get('id'),
                    name: route.get('name'),
                    user: '',
                    pois: []
                };

                route.get('creator').then((user) => {
                    $roteiro.user = user.get('name');
                });
                
                route.get('pois').then((points) => {
                    console.log(points); //Basically its here where it fails sometimes. 
                    var $i = 0;
                    points.forEach(function(poi){
                        $roteiro.pois.push(poi);
                        $i++;
                        if($i == points.length){
                            var sponsor = {name: 'Munícipio da Lourinhã', route: 'lang.index'};
                            Controller.set('roteiro', $roteiro);
                            Controller.set('sponsor', sponsor);
                        }
                    });
                });
            });   
        }
    }
});

I have to routes, Number 1 and Number 2. Booth has the same poi relationships, if they are different it works has it should, but if they have at least one that is the same, it will not work.

If I refresh the page in route/1 or route/2 it works has it should. But if I move direct from route/1 to route/2 with a link-to, it will not return the poi relations, it return an empty array.

I can’t understand what could this be… its so strange…

I can’t see the issue but I would strongly recommend renaming your model, having a model called “route” is asking for confusion. Maybe use “journey” or “course” instead because “route” has a very specific meaning in Ember.

I don’t think this should cause the issue but it would definitely help people help you!

1 Like