Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecation error when store.push(jsonApiDocument) #4372

Closed
BryanCrotaz opened this issue May 6, 2016 · 8 comments
Closed

deprecation error when store.push(jsonApiDocument) #4372

BryanCrotaz opened this issue May 6, 2016 · 8 comments

Comments

@BryanCrotaz
Copy link
Contributor

BryanCrotaz commented May 6, 2016

call store.push passing a json api document with a relationship inside it:

relName: {
  data: [ {id:6, type:'model-name'} ]
}

Error: cannot pass classes to modelFor, only classnames

Fix is in store.js

function deserializeRecordId(store, key, relationship, id) {
  if (isNone(id)) {
    return;
  }

  assert(`A ${relationship.parentType} record was pushed into the store with the value of ${key} being ${Ember.inspect(id)}, but ${key} is a belongsTo relationship so the value must not be an array. You should probably check your data payload or serializer.`, !isArray(id));

  //TODO:Better asserts
  return store._internalModelForId(/*FIX*/id.modelName || /*FIX*/id.type, id.id); // <===== FIX here
}

I need to work out how to test this before I can submit a PR

@pangratz
Copy link
Member

pangratz commented May 6, 2016

Can you update this ember-twiddle so it demonstrates the issue you're having?

@pangratz
Copy link
Member

pangratz commented May 6, 2016

Your fix of passing id.modelName looks like you are passing an instance of DS.Model to store.push somewhere. Something like:

let otherPerson = store.peekRecord('person', 1);

store.push({
  data: {
    id: 2,
    type: 'person',
    relationships: {
      siblings: {
        data: [ otherPerson ]
      }
    }
  }
});

Can you check if this is the case in your code?

@BryanCrotaz
Copy link
Contributor Author

I've said above what I'm doing - importing a json-api document.

Here's my app code:

// routes/application.js
import Ember from 'ember';
import fixtures from '../fixtures';

export default Ember.Route.extend({
    beforeModel() {
        this.store.push(fixtures);
    }
});

// adapters/application.js
import JSONAPIAdapter from 'ember-data/adapters/json-api';

export default JSONAPIAdapter.extend({
    findRecord() {
        return { data: [] };
    },

    findAll() {
        return { data: [] };
    }
});

// fixtures.js
export default {
    data: [
        {
            id: 1,
            type: 'iptv-group',
            attributes: {
                name: "News"
            },
            relationships: {
                channels: {
                    data: [
                        {id: 6, type: 'iptv-channel'},
                        {id: 11, type: 'iptv-channel'}
                    ]
                }
            }
        },
        {
            id: 2,
            type: 'iptv-group',
            attributes: {
                name: "Sport"
            },
            relationships: {
                channels: {
                    data: [
                        {id: 7, type: 'iptv-channel'},
                        {id: 8, type: 'iptv-channel'},
                        {id: 9, type: 'iptv-channel'},
                        {id: 10, type: 'iptv-channel'},
                        {id: 13, type: 'iptv-channel'}
                    ]
                }
            }
        },
        {
            id: 3,
            type: 'iptv-group',
            attributes: {
                name: "Sky"
            },
            relationships: {
                channels: {
                    data: [
                        {id: 7, type: 'iptv-channel'},
                        {id: 8, type: 'iptv-channel'},
                        {id: 9, type: 'iptv-channel'},
                        {id: 10, type: 'iptv-channel'},
                        {id: 11, type: 'iptv-channel'}
                    ]
                }
            }
        },
        {
            id: 4,
            type: 'iptv-group',
            attributes: {
                name: "UK TV"
            },
            relationships: {
                channels: {
                    data: [
                        {id: 1, type: 'iptv-channel'},
                        {id: 2, type: 'iptv-channel'},
                        {id: 3, type: 'iptv-channel'},
                        {id: 4, type: 'iptv-channel'},
                        {id: 5, type: 'iptv-channel'},
                        {id: 12, type: 'iptv-channel'}
                    ]
                }
            }
        },
        {
            id: 5,
            type: 'iptv-group',
            attributes: {
                name: "All"
            },
            relationships: {
                channels: {
                    data: [
                        {id: 1, type: 'iptv-channel'},
                        {id: 2, type: 'iptv-channel'},
                        {id: 3, type: 'iptv-channel'},
                        {id: 4, type: 'iptv-channel'},
                        {id: 5, type: 'iptv-channel'},
                        {id: 6, type: 'iptv-channel'},
                        {id: 7, type: 'iptv-channel'},
                        {id: 8, type: 'iptv-channel'},
                        {id: 9, type: 'iptv-channel'},
                        {id: 10, type: 'iptv-channel'},
                        {id: 11, type: 'iptv-channel'},
                        {id: 12, type: 'iptv-channel'},
                        {id: 13, type: 'iptv-channel'}
                    ]
                }
            }
        }
    ],
    included: [
        {
            id: 1,
            type: 'iptv-channel',
            attributes: {
                name: "BBC One",
                address: "235.0.0.126",
                port: 5500
            }
        },
        {
            id: 2,
            type: 'iptv-channel',
            attributes: {
                name: "BBC Two",
                address: "235.0.0.125",
                port: 5500
            }
        },
        {
            id: 3,
            type: 'iptv-channel',
            attributes: {
                name: "ITV 1",
                address: "238.1.0.93",
                port: 5500
            }
        },
        {
            id: 4,
            type: 'iptv-channel',
            attributes: {
                name: "Channel 4",
                address: "238.4.0.128",
                port: 5500
            }
        },
        {
            id: 5,
            type: 'iptv-channel',
            attributes: {
                name: "Channel 5",
                address: "238.0.0.93",
                port: 5500
            }
        },
        {
            id: 6,
            type: 'iptv-channel',
            attributes: {
                name: "BBC News 24",
                address: "238.2.0.133",
                port: 5500
            }
        },
        {
            id: 7,
            type: 'iptv-channel',
            attributes: {
                name: "Sky Sports 1",
                address: "237.0.0.0",
                port: 5500
            }
        },
        {
            id: 8,
            type: 'iptv-channel',
            attributes: {
                name: "Sky Sports 2",
                address: "237.0.0.1",
                port: 5500
            }
        },
        {
            id: 9,
            type: 'iptv-channel',
            attributes: {
                name: "Sky Sports 3",
                address: "237.0.0.2",
                port: 5500
            }
        },
        {
            id: 10,
            type: 'iptv-channel',
            attributes: {
                name: "Sky Sports 4",
                address: "237.0.0.3",
                port: 5500
            }
        },
        {
            id: 11,
            type: 'iptv-channel',
            attributes: {
                name: "Sky News",
                address: "237.1.0.4",
                port: 5500
            }
        },
        {
            id: 12,
            type: 'iptv-channel',
            attributes: {
                name: "BBC London",
                address: "237.2.0.33",
                port: 5500
            }
        },
        {
            id: 13,
            type: 'iptv-channel',
            attributes: {
                name: "BT Sport",
                address: "237.4.0.154",
                port: 5500
            }
        }
    ]
};



@BryanCrotaz
Copy link
Contributor Author

PS we use peek...() to read from the store as we only have fixture data

@pangratz
Copy link
Member

pangratz commented May 6, 2016

The payload you posted looks ok. Are you getting the error for the code above?

I can reproduce your "cannot pass classes" error when the data pushed via store.push contains a DS.Model, see this ember-twiddle.

@BryanCrotaz
Copy link
Contributor Author

BryanCrotaz commented May 6, 2016 via email

@pangratz
Copy link
Member

pangratz commented May 6, 2016

I cannot reproduce the error you're getting, pushing the fixture data you provided, see this ember-twiddle. Can you take a look and see what is different to your case?

@pangratz
Copy link
Member

pangratz commented May 25, 2016

@BryanCrotaz I'm inclined to close this issue as it cannot be reproduced. Also, the only way I see how the errors gets thrown, is if you pass a model instance somewhere in the payload passed to store.push. Your proposed fix of also checking for modelName strengthen my suspicion, since modelName is a DS.Model thing...

Please feel free to reopen if you can reproduce this bug. I definitely want to improve the experience here and maybe we can do that by a helpful assertion.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants