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

Cannot read property 'decode' of undefined #1

Closed
michaelgosling opened this issue Apr 16, 2019 · 3 comments
Closed

Cannot read property 'decode' of undefined #1

michaelgosling opened this issue Apr 16, 2019 · 3 comments

Comments

@michaelgosling
Copy link

When attempting to use gtfs-rb, on this line: let feed = GtfsRealtimeBindings.FeedMessage.decode(body);, I get the error TypeError: Cannot read property 'decode' of undefined. This actually wasn't happening with the other repo, but I was getting null values for important information with that one.

@michaelgosling
Copy link
Author

michaelgosling commented Apr 16, 2019

Here's the full code for inspection:

"use strict";
const express = require('express');
const GtfsRealtimeBindings = require('gtfs-rb');
const request = require('request');
const cors = require('cors');

const TRIP_UPDATES_URL = "http://gtfs.halifax.ca/realtime/TripUpdate/TripUpdates.pb";
const ALERTS_URL = "http://gtfs.halifax.ca/realtime/Alert/Alerts.pb";
const VEHICLE_POSITIONS_URL = "http://gtfs.halifax.ca/realtime/Vehicle/VehiclePositions.pb";

let tripUpdates = [];
let vehiclePositions = [];
let alerts = [];

/* Get Trip Updates */
function updateAndSendTripUpdates(origResponse) {
  let posReqSettings = {
    method: 'GET',
    url: TRIP_UPDATES_URL,
    encoding: null
  };
  request(posReqSettings, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      let feed = GtfsRealtimeBindings.FeedMessage.decode(body);
      tripUpdates = [];
      feed.entity.forEach((entity) => tripUpdates.push(entity));
      origResponse.send(tripUpdates);
    }
  });
}

/* Start Server */
const app = express();
const port = 3000;
app.use(cors());

app.get('/', (request, response) => {
  response.send("");
});

app.get('/trip_updates', (request, response) => {
  updateAndSendTripUpdates(response);
});

app.listen(port, (err) => {
  if (err) return console.log('ERROR', err);
  console.log(`Server is listening on port ${port}`);
});

@ciolt
Copy link
Member

ciolt commented Apr 16, 2019

You need to import gtfs-rb by referencing the transit_realtime property. It's a change from the old protobuf.js bindings generator, but this is done to separate namespaces.

Ex: This won't work

const gtfsRB = require('gtfs-rb')
gtfsRB.FeedMessage // won't work

This will:

const gtfsRB = require('gtfs-rb').transit_realtime
gtfsRB.FeedMessage // FeedMessage class, with encode/decode/etc.

What I do is something more like this keep my code from being cluttered with "gtfsRealtimeBindings.transit_realtime....etc" everywhere:

const gtfsRB = require('gtfs-rb').transit_realtime
// Deconstruct these classes from GTFS Realtime Bindings
const { FeedMessage, FeedHeader, FeedEntity, VehiclePosition } = gtfsRB

FeedMessage // valid FeedMessage class with all protobuf message methods

@michaelgosling
Copy link
Author

Ohhh, that works perfectly. Thank you!

@ciolt ciolt closed this as completed Apr 16, 2019
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

2 participants