0
votes

I am attempting to retrieve a single record - the "note" for a user.

This is in a Route:

this.store.find('note', {user_id: App.user.id}).then(function (note) {
    window.console.log(note, arguments);
    App.set('note', note.content[0]);
});

It works fine when a record exists already but I want to create a new record ready for saving later if the API doesn't return anything.

I have read this page in the guide but there's nothing about failure handling: http://emberjs.com/guides/models/finding-records/

What does the REST API need to return when it doesn't find a record?

How do I handle this result properly and specifically, so that when store.find doesn't return a model I can call store.createRecord?

1

1 Answers

0
votes

Found an answer here in a question about promises.

store.find returns a promise so that's what I needed to search for rather than trying to search for how to handle find() failing.

The resultant code is as follows:

store.find('note', {user_id: App.user.id}).then(function (note) {
    App.set('note', note.content[0]);
}, function () {
    App.set('note', store.createRecord('note', {user_id: App.user.id, user: App.user}));
});