0
votes

Apologies in advance, I'm very new to Ember and javascript.

I've been following the tutorial at https://medium.com/@jamesfuthey/a-gentle-introduction-to-ember-2-0-8ef1f378ee4#.mis7qdebs

He's introduced a pattern of creating components and putting all of the actions in the components and routes instead of using a controller. I want to know how to update a record in the database (firebase).

I didn't explicitly create an ID in my model, so I was trying to find a specific record using one of the attributes, but it's not working. I get the error of

Error: no record was found at https://taskline.firebaseio.com/tasks/taskname

Which I think is happening because my syntax for findRecord is wrong. I thought I was following the convention as described in the docs https://guides.emberjs.com/v2.4.0/models/creating-updating-and-deleting-records/

updateTask: function (model) {
  let task = this.store.findRecord('task', 'taskname').then(function(task){
    task.set( 'taskname', model.taskname);
    task.set( 'startdate', model.startdate);
    task.set( 'enddate',  model.enddate);
    task.set( 'banding',  model.banding);
  });
  task.save();
}

then additionally I get an error

Uncaught TypeError: undefined is not a function index.js:26
updateTask

which appears to be thrown by the same function. can anyone redirect me here?

1

1 Answers

1
votes

findRecord is returning a promise object (because it may make an async call to find the record). The then function is where the task is fully loaded. You did set the task properties at the right place (within the then function), but you've called save on the promise instead of the loaded task. Your code should look something like this:

updateTask: function (model) {
    this.store.findRecord('task', 'taskname').then(function(task){
        task.set( 'taskname', model.taskname);
        task.set( 'startdate', model.startdate);
        task.set( 'enddate',  model.enddate);
        task.set( 'banding',  model.banding);
        return task.save();
    });
}

The return statement before the save is just to chain the promise object (save itself is also a promise). So if you would return the promise from the findRecord, any then called on that promise will resolve after the save is done.