javascript - NodeJs/Google Firebase 函数字符串方法不起作用

标签 javascript node.js firebase dialogflow-es actions-on-google

我有一个 Google 助理应用,它使用一些 API 来检索和提供本地大学的公交车到达时间。问题是 API 以字符串形式返回到达时间,如下所示:

“2018-51-02T06:51:11”

我尝试使用 javascript 中存在的 slice 和 indexOf 函数来操作该字符串,以获取字符串的最终时间部分,确切的代码是

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

但最后它仍然只打印出来并以原始字符串响应。在我的机器上离线和本地该代码可以工作,但是当上传到 Firebase Functions 时,它不再工作。对这个问题有帮助吗?

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.");
    });
});

最佳答案

我无法使用 node.js 6 和以下代码在 Firebase Cloud Functions 上重现您的问题:

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

正如预期的那样,它将以下内容发送到日志:

finalstring 06:51:11

如果您显示导致问题的完整代码,我们也许可以帮助您解决问题。

您看到的行为表明“T”实际上不在字符串中。

否则,我通常使用类似这样的代码:

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

(但前提是我知道日期时间中实际上有一个 T)

关于javascript - NodeJs/Google Firebase 函数字符串方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53585739/

相关文章:

javascript - 使用 iframe for youtube 等的 Angular 和 Firebase 数据

mysql - 使用 passport+express+mysql+sequelize 进行用户注册

javascript - Electron 页面导航: Loading content and attached javascript

android - 未找到 : 'dart:html' import 'dart:html' ; I don't need dart:html and I also didn't used, 但我尝试通过导入它,但错误不会发生

Firebase云功能不触发onCreate

node.js - 如何自动安装 npm 对等依赖项?

javascript - iframe 的高度不会使用 CSS 根据外部 div 高度调整大小

javascript - 如何检查字符串是否只包含某些字母,如果包含其他字母,则返回 false

javascript - p :commandButton only works with two clicks (JSF 2. 2 错误?)

javascript - 为什么我的 v-for 循环没有给我 Vue.js 中的数据?