Shoffles
Shoffles

Reputation: 32

NodeJs / Google Firebase functions string methods not working

I have a Google Assistant app that uses some API's to retrieve and deliver bus arrival times for my local university. The thing is the API returns the arrival times in a string like this:

"2018-51-02T06:51:11"

I try to maniuplate that string with the slice and indexOf functions that exist within javascript to just get the final time portion of the string, the exact code is

finalString = departure.slice(departure.indexOf('T')+1, departure.length);

but at the end of it all it still only prints out and responds with the original string. Offline and local on my machine that code works, but when uploaded to Firebase Functions it no longer works. Any help with this issue?

app.intent("wheres the catabus", (conv, {route}) => {
    var routeDetails;
    var closest_stop;
    var finalString;
    return cataAPIService.getRouteDetails(route)
    .then((routeData) => {
        routeDetails = routeData;
        closest_stop = cataAPIService.findClosestStop(routeData, conv.device.location);
        return cataAPIService.getStopDetails(closest_stop.StopId)
    })
    .then((stopData) => {
        var departure = cataAPIService.getEstimatedStopDeparture(routeDetails, stopData);
        finalString = departure.slice(departure.indexOf('T')+1, departure.length);

        conv.ask('The closest stop to you is at ' + closest_stop.Name + '. The next departure is scheduled for ' + finalString);
    })
    .catch((error) => {
        console.log(error);
        conv.ask("I can't get that information right now, please try again.");
    });
});

Upvotes: 0

Views: 261

Answers (1)

Prisoner
Prisoner

Reputation: 50701

I wasn't able to duplicate your problem on Firebase Cloud Functions using node.js 6 and the following code:

var departure="2018-51-02T06:51:11";
var finalString = departure.slice(departure.indexOf('T')+1, departure.length);
console.log('finalstring',finalString);

As expected, it sent the following to the logs:

finalstring 06:51:11

If you show the complete code that is causing the problem, we may be able to help you out.

The behavior you're seeing suggests that the "T" isn't actually in the string.

Otherwise, I typically use code more like this:

var f2 = departure.split('T')[1];

(but only if I know there is actually a T in the datetime)

Upvotes: 1

Related Questions