janhommes / o.js

o.js - client side oData lib.
https://janhommes.github.io/o.js/example/
MIT License
241 stars 58 forks source link

disablePolyfills of undefined #91

Closed returnundefined closed 5 years ago

returnundefined commented 5 years ago

Hi @janhommes Refer #85 . Issue occurs. Please check the usage: (This is in TypeScript Express route)

import odata from 'odata';
const oHandler = odata.o('http://services.odata.org/V2/Northwind/Northwind.svc/');
oHandler.get('Categories').query().then(data => {
        console.log(data);
});

Resulting error:

TypeError: Cannot read property 'disablePolyfill' of undefined

Edit: So I figured out I have to pass the disablePolyfill as config to the constructor. This is what I did

import odata from 'odata';
const oHandler = odata.o('http://services.odata.org/V2/Northwind/Northwind.svc/',{
disablePolyfill:true,
headers:{}
});
oHandler.get('Categories').query().then(data => {
console.log(data);
});

Now the error is: ReferenceError: Headers is not defined This seems to be coming from o.ts itself.

returnundefined commented 5 years ago

@janhommes So to make sure this is not something to do with my application code, I created a basic node JS file that has just these lines of code:

const odata = require('odata');

const oHandler = odata.o('https://services.odata.org/V4/(S(ms4wufavzmwsg3fjo3eqdgak))/TripPinServiceRW/', {
    headers: {
        'If-Match': '*'
    }
});

oHandler.get('People').query().then((data) => {
    console.log(data);
});

When I use the TripPin service as in the example code, the response is a nicely formatted json of People entities When I change that to a Northwind V2 or V4 OData, and access the Categories resource, I get this error

(node:23742) UnhandledPromiseRejectionWarning: #<Response>
(node:23742) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:23742) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Does this library only work with a specific service implementation?

janhommes commented 5 years ago

So, for your first post: I am able to reproduce. There is an issue in the o() function which let it fail if you don't pass a config. You can work around this by passing an empty object:

const oHandler = odata.o('https://services.odata.org/V3/Northwind/Northwind.svc/', {});

The second is just a configuration issue. The northwind v4 can not handle the If-Match header and needs to add a query parameter to return json:

const odata = require('odata');
const oHandler = odata.o('https://services.odata.org/V3/Northwind/Northwind.svc/', {});

oHandler.get('Categories').query({'$format': 'json'}).then((data) => {
    console.log(data);
}).catch((ex) => {
  console.log(ex);
});

Will fix the first issue as soon as possible.

janhommes commented 5 years ago

the "disablePolyfills of undefined" issue is resolved in 1.0.3