I am trying to write a simple app that utilizes the GTFS (General/Goodle Transit Feed Specification) to retrieve alerts regarding conditions on the road. I have managed to get the code snippet which I am working with to work with vanilla js and typescript, but so far I have not managed to get it to work with react native, which is specifically what I would need it to work with. Below is a snippet of the code that I use at the moment (that works with ts and js)
const url = CONSTANTS.API_URL;
protobuf.load('protocol.proto', (err, root) => {
if (err) {
throw err;
}
const TransitRealtime = root.lookupType('transit_realtime.FeedMessage');
fetch(url)
.then(response => response.arrayBuffer())
.then(buffer => {
const message = TransitRealtime.decode(new Uint8Array(buffer));
const object = TransitRealtime.toObject(message, {
longs: String,
enums: String,
bytes: String,
});
console.log(object.entity[1].alert.headerText);
console.log(object.entity[1].alert.descriptionText);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
});
My react-native project uses .js as the file format.
I have the default GTFS protocol .proto file located in android/app/src/main/assets at the moment and the code should be able to access it (I tried to use RNFS to read and display the contents in console which worked fine).
The baffling thing to me is that the word ‘Expected’ does not exists anywhere in my whole workspace. This is also my first time using protocol buffers. I use protobufjs to handle them.