A friend's query was failing. Fortunately, he was catching it in his fail callback (you DO have a fail callback for every server call, right?). Here's kind of what he had:
var getPersons = function(personsObservable) { return EntityQuery.from('Person') .using(manager).execute() .then(querySucceeded).fail(queryFailed); } function queryFailed(error) { var msg = 'Error retreiving data. ' + error.message; logError(msg, error); throw error; }
The error.message
simply showed the JSON data ... which looked a bit like this:
"[{"$id":"1","$type":"Person, ProjectName","Id":12,"FirstName":"Bob","LastName":"Smith","Email":"[email protected]","Blog":"http://bs.contoso.com","Twitter": ..."
WAT?
He examined the error.XHR
which provides the full AJAX XHR object used for this query. He could see that the HTTP Status Code was a 200 ... meaning that everything was cool from the server. The fact that he had real data pretty much said the same thing.
So why is Breeze failing? How does he diagnose the problem?
Breeze might be failing. But there's a good chance that the problem lies elsewhere. Usually if Breeze fails, there is a meaningful error message. This error message is not meaningful. But it does provide clues.
The fail callback can be invoked (1) if the operation fails or (2) if the success callback fails. If the operation fails, you've got a Breeze-related problem. If the success callback fails, you probably have an application code problem.
To determine which, put a breakpoint on the first line of the success callback (in his case, the first line of querySucceeded
). If you hit the breakpoint, you know Breeze has done its bit and has handed off to you. Step through your callback to find the mistakes which are most likely yours and, therefore, easy to fix.
In his case it did not get to the success callback. So something went wrong as Breeze tried to make cached entities out of the JSON data from the server. What could that be?
There are many potential causes. Could be a Breeze bug. Always best, though, to eliminate pilot error first. Did you write a custom constructor or initializer for this EntityType
?
He did. He had an initializer that added a fullName
calculated property to his Person
. It looked sort of like this:
metadataStore.registerEntityTypeCtor('Person', null, personInitializer); function personInitializer(person) { person.fullName = ko.computed(function () { return entity.firstName() + ' ' + person.lastName(); }); }
He didn't see a problem. But following diagnostic procedure, he put a breakpoint on the initializer.
Sure enough ... he had a typo ...
// "entity" does not exist. Null object error return entity.firstName() + ' ' + person.lastName();
As soon as he changed entity
to person
, all was well.
I can't explain at the moment why the null object reference manifested as a Q promise fail
error with the JSON Person
data in the message. Strange stuff happens in JavaScript. But the clues were there:
Person
Person
initializer (or constructor)Read the clues and you'll know where to look.
Hope this tip saves you from gray hair and a bald head.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With