【问题标题】:How to use if else loop in Dialogflow for showing results如何在 Dialogflow 中使用 if else 循环来显示结果
【发布时间】:2020-02-03 09:46:55
【问题描述】:

我有 Dialogflow 的 Javascript 代码在 Google Actions 上做项目。对于此代码,如果答案在数据库中,则意味着它将回答,否则将退出应用程序。所以,我想对这段代码使用 else 循环,请帮助我

    function handleCompanyDetails(agent){   
  const RegNo = agent.parameters.RegNo;
  var ref8 =  admin.database().ref().child("Table/");
  var query8 = ref8.orderByChild("RegNo").equalTo(RegNo);
 return query8.once("value")
  .then(function(snapshot) {  
   snapshot.forEach(function(child) {

    if( !snapshot.exists() ){
      // There are no results, say so
      agent.add("There are no results for that account.");

    } else {
      // ... Do something with the data
         agent.add(`The student placed in  ` + child.val().CompanyName);
    }

  });

 });        
 }

【问题讨论】:

    标签: javascript firebase dialogflow-es actions-on-google dialogflow-es-fulfillment


    【解决方案1】:

    虽然您可以使用循环来显示结果,但您的操作方式存在一些问题,甚至可能与您尝试返回的内容有关。

    首先 - Dialogflow 要求您从任何进行异步调用的函数(例如对 Firebase 数据库的调用)返回 Promise。您当前正在使用回调方法。您应该改用返回 Promise 的 once(),这样可能看起来像这样:

    return query8.once("value")
      .then( snapshot => {
        // working with snapshot goes here
      })
      .catch( err => {
        console.error(err);
        agent.add("There was a problem.");
      });
    

    第二个是你如何使用snapshot 本身。如果您期待多个结果,您应该知道您只能使用短信两次和一张基本卡致电agent.add()。如果您想要多张卡片,您可能需要使用list 或轮播。

    如果您期望只有一个响应从 RegNo 索引,看起来您可能是,那么您应该将其作为路径的一部分并获取快照的值。在这种情况下,您不需要循环。

    更新基于更新的代码。

    如您所述,如果没有结果,您不会发送任何内容,因此操作会因错误而退出。

    最简单的方法是使用snapshot.exists() 检查快照中是否有任何结果。如果没有,那么您可以返回错误。这可能看起来像

    return query8.once("value")
      .then(function(snapshot) {  
    
        if( !snapshot.exists() ){
          // There are no results, say so
          agent.add("There are no results for that account.");
    
        } else {
          // ... Do something with the data
        }
    
      });
      // ...
    

    如果您确实有结果,您仍然会遇到回复过多的问题。除非您使用列表或轮播,否则您只能拥有一张 agent.add() 带有要说出的消息(或最多两张,但不要这样做)并且只有一张卡片。因此,您最好在循环中构建该消息。

    【讨论】:

    • 如果我说没有在数据库中列出的 regno,我会遇到同样的错误,它表示我的应用程序没有响应并退出应用程序
    • 请更新您的问题以添加您正在使用的最新代码以及错误的文本记录 - 提供视频会使我们更难为您提供帮助。
    • 还是一样的错误,如果我说错了,更新代码后退出应用程序还是一样
    • 请使用您的最新代码和您可能包含的任何日志更新您的问题。
    【解决方案2】:

    在对快照执行操作时使用 .then()。因为 .once() 只触发一次,所以如果数据可用,则意味着 .then() 将被执行,您可以使用 .catch() 退出它。检查下面的代码。

    function handleCompanyDetails(agent){   
      const RegNo = agent.parameters.RegNo;
      var ref8 =  admin.database().ref().child("Table/");
      var query8 = ref8.orderByChild("RegNo").equalTo(RegNo);
      return query8.once("value")
         .then(function(snapshot) {  
              snapshot.forEach(function(child) {
                  agent.add(`The student placed in  ` + child.val().CompanyName);
                  agent.add(new Card({
                      title: ` Name:${child.val().Studentname}
                      Reg No: ${child.val().RegNo}
                      Offer Date: ${child.val().OfferDate} `, 
                      imageUrl: '',
                      text:  `Thanks for using ?\n  ${child.val().FirstName} ?`,
                      buttonText: '.com'
                  })
               })
            })
         .catch( // throw some error)
        }
    
    

    您可以在这里阅读更多内容,https://firebase.google.com/docs/database/web/read-and-write

    【讨论】:

    • 代码现在可以工作了,但是如果我说使用 reg no 获取详细信息,如果我说错误的数字应用程序存在并且不处理错误,它会填充 fetch
    • 我没听懂你想说的话。你能用通俗易懂的语言解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2012-09-02
    • 2021-09-05
    相关资源
    最近更新 更多