Finding Records is not working


#1

My Code :

App = Ember.Application.create();

App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.Student=DS.Model.extend({
  name:DS.attr('string'),
  projects:DS.hasMany('project',{async:true})
});

App.Project=DS.Model.extend({
  name:DS.attr('string'),
  student:DS.belongsTo('student')
});

App.Student.FIXTURES = [
 { id: 1, name: 'Kris', projects: [1,2] },
 { id: 2, name: 'Luke', projects: [3] }
];

App.Project.FIXTURES=[
 { id:1,name:'Proj1',student:1 },
 { id:2,name:'Proj2',student:1},
 { id:3,name:'Proj3',student:2}
]; 

App.IndexRoute = Ember.Route.extend({
 model: function() {   
   return this.store.find('student');
 }
});

App.IndexController=Ember.ArrayController.extend({
  actions:{
    saveRecord:function(){
    var stu1=this.store.find('student',1);
    console.log(stu1.get('name'));  // RETURNS Undefined
  }
 }
});

In the IndexController finding records by id is not working. Its returns undefined. What wrong with my code ?


#3

this.store.find returns a promise.

At the time you try to log your results, the promise has probably not been fulfilled yet.

Try :

this.store.find('student',1).then(
  function(stu1) {
    console.log(stu1.get('name'));
  },
  function(error) {
  // handle error
  }
);