BlairAllegroTech / js-data-jsonapi

JsonApi Adapter for js-data
MIT License
15 stars 5 forks source link

createInstance fails with Option cacheResponse: false #20

Closed rgant closed 8 years ago

rgant commented 8 years ago

Setting the option cacheResponse to false on a .find operation results in the error ""attrs" must be an object!" from js-data.

find call:

Users.find('me', {
    headers: {
        'Authorization': 'Basic ' + LoginService.isLogin()
    },
    cacheResponse: false
}).then(
    function success(usr){console.log(usr);}, 
    function fail(err){console.error(err);}
);

Tracing through the code it appears that the cacheResponse flag is checked here in js-data:

if (options.cacheResponse) {
    // injected created intem into the store
    var created = _this.inject(definition.name, attrs, options.orig());
    var id = created[definition.idAttribute];
    // mark item's `find` query as completed, so a subsequent `find` call for this item will resolve immediately
    var _resource = _this.store[resourceName];
    _resource.completedQueries[id] = new Date().getTime();
    _resource.saved[id] = DSUtils.updateTimestamp(_resource.saved[id]);
    return created;
} else {
    // just return an un-injected instance
    return _this.createInstance(resourceName, attrs, options);
}

If the code takes the 'true' path then there is no error, however if to tries to call createInstance directly then attrs is an [Object Array] instead of an [Object Object] and the call to DSUtils.isObject rejects it.

BlairAllegroTech commented 8 years ago

Yes i can see the problem here.. I have created many adapter tests but also need to create tests that interact via the data store also!!

At the moment the adapter always returns an array back to js-data. For single objects this must be an object. This applies to all responses to non xxxxAll requests

rgant commented 8 years ago

I am not certain where in the code this all gets handled. If I can be of any help to you, please just point me in the right direction. As always, thanks so much!

BlairAllegroTech commented 8 years ago

I started looking at this. BUT will be a few hours before I get back to it. I have to do some paid work as well.

One question. Are you developing an open source project or work related. Would be interested to see it. As you can probably tell. I haven't got a reference project that uses this. I have started work on one but have still got a fair way to go on it.

rgant commented 8 years ago

I'm working on an app to help charitable organizations and nonprofits calculate their impact with the idea being that they can build a social network of such organizations to help identify and share best practices and to help get more funding. The current project is our web application for funders and grantees, but it's backed by an API that we want to open up to other organizations doing charitable work.