简体   繁体   English

错误:agent.add() 函数不起作用,即无法向用户获取输出值。在 Google 助手中

[英]Error : agent.add() function is not working i.e not getting output value to user.In Google assistant

Following code is working.以下代码正在运行。 But agent.add() function is not working, ie not getting output value to user.但是agent.add()函数不起作用,即没有向用户获取输出值。

var ref = admin.database().ref().child("Table/"); 
var query = ref.orderByChild("RegId").equalTo(RegId.toString()); 
query.once("value", function(snapshot) { snapshot.forEach(function(child) {
  console.log(child.key);
  console.log("FirstName: " + child.val().FirstName); 
  console.log("Mobile: " + child.val().MobileNumber); 
  console.log("Email: " + child.val().EmailId);
  var name = snapshot.child("FirstName").val(); 
  agent.add(The student name is ` `+` name);`
});

agent.add() is not working but, console.log() is working fine in database logs. agent.add()不工作,但console.log()在数据库日志中工作正常。

While agent.add() does have some syntax problems in it, that isn't the core of your problem.虽然agent.add()确实存在一些语法问题,但这不是问题的核心。

You also seem to be getting a child of the snapshot called "FirstName" each time you go through the loop, which I don't think is what you want to do.每次遍历循环时,您似乎也会得到名为“FirstName”的快照的子项,我认为这不是您想要做的。 But this also isn't the core of the problem.但这也不是问题的核心。

The issue is that query.once() is an asynchronous call, and you're using a callback function to handle it.问题是query.once()是一个异步调用,您使用回调函数来处理它。 The Actions on Google library expects you to return a Promise to indicate you are making asynchronous function calls, so it knows not to return anything to the user until the call is completed. Actions on Google 库期望您返回一个 Promise 以指示您正在进行异步函数调用,因此它知道在调用完成之前不会向用户返回任何内容。

The best way for you to do this is to have query.once() return a Promise, handle your processing in the .then() portion of the Promise, and to return the Promise/then chain in your handler.你要做到这一点,最好的办法是让query.once()返回一个承诺,在处理您处理.then()的承诺的一部分,并在你的处理器返回无极/然后链。

I haven't tested it, but it might look something like this:我还没有测试过,但它可能看起来像这样:

return query.once("value")
  .then( snapshot => {
    snapshot.forEach( child => {
      let name = child.val().FirstName;
      console.log( 'FirstName: ' + name );
      agent.add( 'The student name is ' + name );
    });
  });

This line has invalid syntax:此行的语法无效:

agent.add(The student name is ` `+` name);`

Should be quoted properly:应该正确引用:

agent.add('The student name is ' + name);
// or if you're using es6:
agent.add(`The student name is ${name}`);

Look into a good code editor that highlights these errors for you.查看一个好的代码编辑器,为您突出显示这些错误。 I use vscode.我使用vscode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dialogflow agent.add 无法正常工作 - Dialogflow agent.add not working properly 如何在 function 中添加一个循环,它可以在 Dialogflow 中调用 agent.add 几次? - How can I add a loop inside a function which could invoke agent.add couple of times in Dialogflow? agent.add() 不工作,虽然 console.log() 是 - agent.add() not working, though console.log() is agent.add 在 promise.then 中不起作用 - agent.add not working inside promise.then dialogflow speack函数,即无法正常工作的js - the speack function i.e articulate js which is not working ScrollIntoView 在 reactjs 中不适用于 IE? - ScrollIntoView is not working for I.E in reactjs? 如何处理对Firestore的Promise调用并调用agent.add() - How to handle a Promise call to Firestore and call agent.add() 展平数组,即[[1、2、3],[4、5],[6]]的输出应为[1、2、3、4、5、6]。 代码中的错误源 - Flatten an array, i.e [[1, 2, 3], [4, 5], [6]] should give an output of [1, 2, 3, 4, 5, 6]. The source of error in the code Facebook FB.ui SDK功能无法在IE中正常运行 - Facebook FB.ui SDK function not working properly in I.E 如何在列表中添加到onclick函数,即<li onclick=“”><button onclick=“”></button></li> - How to add to onclick function inside a list i.e<li onclick=“”><button onclick=“”></button></li>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM