Reputation: 627
I would like to use the any of the Feed URLS on this page https://www.mbta.com/developers/gtfs-realtime
I added the dependency in a new project folder. then created a file app.js with the code snippet from the github project page https://github.com/google/gtfs-realtime-bindings/tree/master/nodejs
So my app.js file looks like this...
var GtfsRealtimeBindings = require('gtfs-realtime-bindings');
var request = require('request');
var requestSettings = {
method: 'GET',
url: 'http://developer.mbta.com/lib/GTRTFS/Alerts/TripUpdates.pb',
encoding: null
};
request(requestSettings, function (error, response, body) {
if (!error && response.statusCode == 200) {
var feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(body);
feed.entity.forEach(function (entity) {
if (entity.trip_update) {
console.log(entity.trip_update);
}
});
}
});
However, I keep getting the following error message when I type 'node app.js'
TypeError: Cannot read property 'FeedMessage' of undefined
at Request._callback (C:\wamp64\www\dev\gtfs\app.js:11:57)
at Request.self.callback (C:\wamp64\www\dev\gtfs\node_modules\request\request.js:186:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (C:\wamp64\www\dev\gtfs\node_modules\request\request.js:1163:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (C:\wamp64\www\dev\gtfs\node_modules\request\request.js:1085:12)
at IncomingMessage.g (events.js:291:16)
at emitNone (events.js:91:20)
Any clues as to what I am missing here, or doing wrong?
Also any idea what the "gtfs-realtime.proto" file is used for? Whether/where I should include it in my project folder? https://developers.google.com/transit/gtfs-realtime/gtfs-realtime-proto
Thanks, I appreciate your help, this is my first time parsing gtfs feeds.
Upvotes: 1
Views: 1125
Reputation: 7775
The example code on that page seems to be wrong. Replace
var feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(body);
with
var feed = GtfsRealtimeBindings.FeedMessage.decode(body);
and the code will work as expected.
The gtfs-realtime.proto
file describes the format of the GTFS real-time messages, and how they are to be encoded into a compressed binary protobuf message. The node package you are using to read the protobuf (gtfs-realtime-bindings
) already takes care of decoding the protobuf for you, so you don't need to include the proto file yourself.
Upvotes: 1