jbrown
jbrown

Reputation: 7986

Emberjs: How to tell if a record wasn't found?

I've got an app where I need to query for a related record that may or may not exist. If it exists, I need to update the value. If not, I need to create it.

How can I use this.store.find to handle the case where the model doesn't exist?

E.g. my code is:

// todo: now save this answer into the completed survey
self.store.find('answer', {completed_survey: completedSurvey,
                           question: question}).then(function(answer) {
  answer.set('value', formAnswerText);
  answer.save();
});        // what if there was no object found though?

Upvotes: 0

Views: 171

Answers (1)

Billybonks
Billybonks

Reputation: 1568

either you have a error handler

.then(function(model){},function(error){})

or you just check if its null

if(model){
//found
}else{
//notfound
}

Upvotes: 1

Related Questions