Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TripRequest not working #29

Open
iKK001 opened this issue Jul 16, 2023 · 1 comment
Open

TripRequest not working #29

iKK001 opened this issue Jul 16, 2023 · 1 comment

Comments

@iKK001
Copy link

iKK001 commented Jul 16, 2023

I tried your ojp-js library. Great work by the way !!

And unfortunately, I am still doing something wrong, possibly, when trying to get a TripRequest.

Maybe you can help me.

Here is my example code :

import * as OJP from "ojp-sdk"

const fromLoc = OJP.Location.initWithStopPlaceRef("8507000", "Bern")
const toLoc = OJP.Location.initWithStopPlaceRef("8503000", "Zurich")
const fromLocation = new OJP.TripLocationPoint(fromLoc)
const toLocation = new OJP.TripLocationPoint(toLoc)

const tripRequestParams = OJP.TripsRequestParams.initWithLocationsAndDate(
    fromLocation,
    toLocation,
    new Date(),
)

if (tripRequestParams) {
    const stage = {
        key: "SBB",
        apiEndpoint: "https://api.opentransportdata.swiss/ojp2020",
        authBearerKey:
            "57c5dbbbf1fe4d0001000018e0f7158cb2b347e3a6745e3ef949e7bf",
    }
    const tripRequest = new OJP.TripRequest(stage, tripRequestParams)
    console.log("hmmmmmmmmm 1")
    tripRequest.fetchResponse((responseText: any, errorData: any) => {
        console.log("hmmmmmmmmm 2")
        if (errorData) {
            console.error(errorData)
        }
        console.log(responseText)
    })
}

And here is the error log:

[BUILD] hmmmmmmmmm 1
[BUILD] <userDir>/test/node_modules/ojp-sdk/lib/trips/trips-response.js:12
[BUILD]         const responseXML = new DOMParser().parseFromString(responseXMLText, 'application/xml');
[BUILD]                             ^
[BUILD] 
[BUILD] ReferenceError: DOMParser is not defined
[BUILD]     at Function.initWithXML (<userDir>/test/node_modules/ojp-sdk/lib/trips/trips-response.js:12:29)
[BUILD]     at <anonymous> (<userDir>/test/node_modules/ojp-sdk/lib/request/trips-request/trips-request.js:14:49)
[BUILD]     at <anonymous> (<userDir>/test/node_modules/ojp-sdk/lib/request/base-request.js:61:17)
[BUILD]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[BUILD] yarn tsx src/index.ts exited with code 1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

As you can see, it never reaches the hmmmmmmmmm 2 callback.

It obviously asks for a DOMParser() - but what does that mean ?

I do not want to make a frontend-App. But rather use it inside a Raspberry Pi or similar simply to get trips from A to B.

@iKK001
Copy link
Author

iKK001 commented Jul 16, 2023

I have a workaround:

If you add the following, then it works!

import jsdom from "jsdom"

const { JSDOM } = jsdom
global.DOMParser = new JSDOM().window.DOMParser

Obvously also adding "jsdom": "^22.1.0", and "@types/jsdom": "^21.1.1", to your package.json dependencies...

However, I still think the DOM should not be necessary in the first place. Or am I wrong ? And if yes, why ? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant