Reputation: 882
This is my Ember inspector info tab:
I am using ember-cli-mirage and I'm facing issue here.
I am not able to get belongsTo relationship of a model. When I do car.get('user')
, I get a promise in result which gets fullfilled but the value is always null
.
My user model import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('String'),
car: DS.belongsTo('car',{async: true})
});
My car model
import DS from 'ember-data';
export default DS.Model.extend({
color: DS.attr('String'),
user: DS.belongsTo('user',{async: true})
});
my mirage/config
this.get('/cars',function(db,request) {
var qp = request.queryParams.searchString.toLowerCase();
return {
cars: db.cars.where({'color':qp}),
users: db.users
};
});
I get the list of cars with search color but I don't get the user. it returns a promise which when fullfilled gives null.
Since I am using ember-data 1.13.8
I tried using this Mirage working with json but then I get error datum model is not defined error so i guess new json api is not my issue for some reason my ember data uses old json format and original config works.
this.get('/cars', function(db, request) {
return {
data: db.cars.map(attrs => (
{type: 'cars', id: attrs.id, attributes: attrs }
))
};
})
My cars route look like this
model: function(params) {
self = this;
if(!params){
return [];
}
return this.store.findQuery('car',params);
},
I tried
return this.store.findQuery('car',params).then(function(item){
console.log(item.get('user'));
});
but i still got console.log //undefined
I can see user_id in my json returned.
SOLUTION:
found the issue i was saving relationship as user_id = user.id.
It should be user=user.id
Upvotes: 0
Views: 177
Reputation: 882
SOLUTION: found the issue i was saving relationship as user_id = user.id
. It should be user=user.id
Upvotes: 0