且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Dialogflow:如何在Firebase查询中传递参数?

更新时间:2023-02-06 21:03:56

通常,***的处理方式这是在设置查询路径时引用总线号的节点,一旦得到结果肯定可以得到它,但这意味着您要提取 lot 个数据

In general, the best way to handle this is to reference the node of the bus number as part of setting up the path to the query. Getting it once you have the result is certainly possible, but means you're pulling in a lot more data than you need to for each query.

但是有几种方法可以做到这一点。

But there are a few ways to do this.

与您现在的操作最相似的是生成一个包含路由号的字符串,该示例说明了如何使用反引号来实现此功能,最新的JavaScript中提供了反引号,或者您也可以进行字符串连接:

The one most similar to how you're doing it now is to generate a string that includes the route number. This example shows how to do it using a back-quote, which is available in the most recent JavaScript, or you can just do string concatenation:

function handleBus(agent) {
    const bus = agent.parameters.bus;

    agent.add(`Thank you...`);
    return admin.database().ref('Routes').once("value").then((snapshot) => {
      var routeInfo = snapshot.child(`${bus}/route_desc`).val();
      var routeName = snapshot.child(`${bus}/route_long_name`).val();
      agent.add(`Bus info is ` + routeInfo + ' and is called ' + routeName);

但是如果您只是从中查找信息route,您可以设置对数据库的引用以包括该路由,获取整个结果及其值,然后将其视为JavaScript对象。

But if you're just looking for the information from that route, you can setup the reference to the database to include the route, get the entire result and its value, and then treat this as a JavaScript object.

function handleBus(agent) {
    const bus = agent.parameters.bus;

    agent.add(`Thank you...`);
    return admin.database().ref('Routes').child(bus).once("value").then((snapshot) => {
      var route = snapshot.val();
      var routeInfo = route['route_desc'];
      var routeName = route['route_long_name'];
      agent.add(`Bus info is ` + routeInfo + ' and is called ' + routeName);

顺便说一句,我想指出的是,您完美地使用了Promises。这是很多人陷入的陷阱,您已经很好地通过Promise查询了值,将其作为Promise履行的一部分进行处理,并在处理程序中返回Promise。

As an aside, I want to point out that you're using Promises perfectly. That is a trap many people fall into, and you've done a good job querying the value through a Promise, handling it as part of Promise fulfillment, and returning a Promise in your handler.