Closed techniq closed 9 years ago
I was starting this test to understand how to transform data from a http request (basically how dates using ISO strings are handled. Are they still strings, or converted to Date
instances? I've had the need to modify these timestamps on occasion as was kicking the tires of js-data. I currently have a custom Angular data-layer / services that do similar what js-data does with regards to caching, but I liked the more standard/non-angular approach js-data has taken.
Hmm, the issue doesn't appear to be specific to jspm / es6 modules after all. I setup an environment using only npm / CommonJS / mocha (instead of karma) and am still getting the same error from js-data-http
test.js
var JSData = require('js-data');
var DSHttpAdapter = require('js-data-http');
var sinon = require('sinon');
describe("JSData Suite", function() {
beforeEach(function() {
this.xhr = sinon.useFakeXMLHttpRequest();
this.requests = [];
this.xhr.onCreate = function (xhr) {
console.log('request found', xhr);
this.requests.push(xhr);
console.log('request pushed');
};
this.store = new JSData.DS();
this.store.registerAdapter('http', new DSHttpAdapter(), { default: true } );
this.User = this.store.defineResource('user');
});
afterEach(function() {
// Restore the global timer functions to their native implementations
this.xhr.restore();
});
it("User resource", function(done) {
this.User.find(1).then(function(data) {
console.log('RESPONSE');
console.log(data);
expect(data).to.exist;
expect(data.id).to.equal(1);
done();
});
setTimeout(function() {
console.log('responding...');
this.requests[0].respond(200, { "Content-Type": "application/json" }, JSON.stringify({id: 2, name: 'Sean'}));
}, 400);
});
});
error
Unhandled rejection TypeError: Cannot read property 'method' of undefined
at logResponse (/Users/smlynch/Development/playground/js-data-test/node_modules/js-data-http/dist/js-data-http.js:184:68)
package.json
{
"devDependencies": {
"mocha": "^2.2.5",
"sinon": "^1.15.3"
},
"dependencies": {
"js-data": "^1.8.0",
"js-data-http": "^1.2.3"
}
}
I think I know what the error is, unfortunately I'm extremely busy at work the next 24 hours, so I'll have to fix it over the weekend. If you want to submit a pull request there needs to be a null check here: https://github.com/js-data/js-data-http/blob/master/src/index.js#L101
Sorry about the noise. Turns out the issue was a syntax error in hiding.
this.requests = [];
this.xhr.onCreate = function (xhr) {
this.requests.push(xhr);
};
should have been (using ES6 arrow function)
this.requests = [];
this.xhr.onCreate = (xhr) => this.requests.push(xhr);
or (ES5)
var requests = this.requests = [];
this.xhr.onCreate = function (xhr) {
requests.push(xhr);
};
Odd error to be raised due to this.requests === undefined
. Thanks for getting back with me. Now I can get back to evaluating JSData for my needs :)
I'm attempting to use js-data with JSPM and was starting by writing a test to understand how js-data / js-data-http works but have already ran into an issue. Below is the test I'm attempting to run with Karma / Chrome
and the output:
Here is my
package.json
:The primary error seems to come from
this.requests.push(xhr);
causing the error:I'm using js-data* 1.x as the 2.x betas (which are installed by default off npm if I didn't add
@1
) were giving me an error onnew DSHttpAdapter()
if I didn't pass an empty options object (ie.new DSHttpAdapter({})
)