2

I keep getting this error in my app but have no idea what is causing it.

It happens whenever I commit my data store:

Attempted to handle event loadedData on while in state rootState.loaded.updated.inFlight. Called with undefined

Anyone?

Here is the code that causes it:

var ts_setting;

ts_setting = Cluey.Setting.find(Cluey.SettingsKeyIDs.API_TIMESTAMP);

if (ts_setting.get('value') != null) {
  console.log("Found Timestamp");
} else {
  console.log("Creating Initial Timestamp...");
  ts_setting.set("id", Cluey.SettingsKeyIDs.API_TIMESTAMP);
  ts_setting.set("value", 0);
  Cluey.store.commit();
}

Edit

I have boiled it down to the following code (written in coffeescript) that is causing the error. The thing is, that the first time I run the code, when the object first does not exist in the data store, it runs fine. The error then happens when I run the code on a data store that already contains a record with the specified id. This might help you decipher what is happening.

ts_setting = Cluey.Setting.find(Cluey.SettingsKeyIDs.API_TIMESTAMP)
ts_setting.get('value')
ts_setting.set("id", Cluey.SettingsKeyIDs.API_TIMESTAMP)
ts_setting.set("value", 0)
Cluey.store.commit()

Edit 2

I am having a similar problem creating a record:

ts_setting = Cluey.Setting.createRecord
    id: Cluey.SettingsKeyIDs.API_TIMESTAMP,
    value: 0

Cluey.store.commit()

The above code gives me this error:

Uncaught Error: Attempted to handle event `loadedData` on <Cluey.Setting:ember327:1> while in state rootState.loaded.created.inFlight. Called with undefined

Edit 3

So it turns out I was calling @timestamp = ts_setting.get('value') just after committing the store which I suppose was causing the issue, as I was trying to fetch some data from an object that had not yet been saved.

l33z3r
  • 617
  • 5
  • 16
  • Could you add the code that is producing this error message? The error is related to states. – MilkyWayJoe Apr 24 '13 at 15:10
  • I have added the code above – l33z3r Apr 24 '13 at 16:28
  • I don't think this code is causing this issue. What are you doing after `commit`? Are you redirecting to a route that is supposed to display all records or somehow retrieve records from the database? It seems to me that you're trying to fetch the record from backend while it hasn't been committed yet. – MilkyWayJoe Apr 24 '13 at 18:39
  • I have updated the original post with the code at its minimal form which is causing the error. – l33z3r Apr 25 '13 at 10:36

1 Answers1

6

Loading data is asynchronous. Cluey.Setting.find returns you a promise that will be resolved/updated once the XHR request succeeds.

So you get your promise when calling Cluey.Setting.find, modify it (ts_setting.set("value", 0)) then at some point in time, you get the result from your server (the response from find).

At that moment, ember data raises an error because it can't update a record that is being modified.

What you want to do is to wait for your record to get completely loaded before modifying and saving it.

ts_setting = Cluey.Setting.find(Cluey.SettingsKeyIDs.API_TIMESTAMP);
ts_setting.one('didLoad', function() {
  ts_setting.set("value", 0);
  Cluey.store.commit();
});
Giovanni Cappellotto
  • 4,597
  • 1
  • 30
  • 33
Cyril Fluck
  • 1,561
  • 7
  • 9
  • I am having a similar problem with creating a record, I have updated the original post with the offending lines of code – l33z3r May 07 '13 at 14:50
  • Do you get this error when typing those lines in the console when no records have been modified? – Cyril Fluck May 07 '13 at 14:57
  • No, strangely enough when I type directly to the console I do not get this error – l33z3r May 07 '13 at 15:17
  • 1
    So my guess is that you your `id` is not unique. You may already have a record with the specific `Cluey.SettingsKeyIDs.API_TIMESTAMP` id. What kind of value is it? – Cyril Fluck May 07 '13 at 15:19
  • It is an integer. I will investigate if that is the case, but It happens when I run that code on an empty localStorage – l33z3r May 07 '13 at 15:22
  • Technically, the source of the problem is the same: `commit` is asynchronous, so you try to modify a record that is being saved. – Cyril Fluck May 07 '13 at 15:27