sports-alliance / sports-lib

A Library for processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc
GNU Affero General Public License v3.0
149 stars 21 forks source link

"Cannot read property 'length' of undefined" while parsing attached GPX file #69

Closed thomaschampagne closed 3 years ago

thomaschampagne commented 3 years ago

Hey Dimi!

Small issue while parsing file _mytrailexport.gpx (mytrail_export.zip). It produces the following error: TypeError: Cannot read property 'length' of undefined

Error is raised at importer.gpx.ts:49:136:

 const endDate = isActivity ?
          new Date(trackOrRoute.trkseg[trackOrRoute.trkseg.length - 1].trkpt[trackOrRoute.trkseg
[trackOrRoute.trkseg.length - 1].trkpt.length // HERE !
 - 1].time[0]) :
          new Date(startDate.getTime() + samples.length * 1000);

Here a jasmine which reproduce the bug on the file.

    it('should parse a MyTrail GPX file', done => {

      // Given
      const path = __dirname + '/fixtures/others/mytrail_export.gpx';
      const gpxString = fs.readFileSync(path).toString();

      // When
      const eventInterfacePromise = SportsLib.importFromGPX(gpxString);

      // Then
      eventInterfacePromise.then((event: EventInterface) => {
        expect(event.getFirstActivity().type).toBeDefined();
        done();
      });

Not sure how to fix it properly. Let me know if you need more investigation from me.

(File is coming from my elevate app testers)

jimmykane commented 3 years ago

Roger that. Let me fix

jimmykane commented 3 years ago

The issue is found.

The gpx contains a trkseg that is empty as the last

    <trkseg>
    </trkseg>

I ll see for a solution

thomaschampagne commented 3 years ago

Let me know if you need anything from me :)

jimmykane commented 3 years ago

Should be fixed in 5.3.0 coming up asap on npm.

keep me posted if there are any side effects. I ll push this to production from my side for QS asap