Query failed and the error.message is the data

有些话、适合烂在心里 提交于 2019-12-12 10:59:01

问题


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":"bs@contoso.com","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?


回答1:


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.

Check your success callback first

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.

Check your custom EntityType constructors and initializers

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:

  • server delivered the data
  • failed before getting to the success callback
  • data are about Person
  • have a 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.



来源:https://stackoverflow.com/questions/14780735/query-failed-and-the-error-message-is-the-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!