错误:agent.add()函数无法正常工作,即无法获取用户的输出值。在Google助手中 [英] Error : agent.add() function is not working i.e not getting output value to user.In Google assistant

查看:88
本文介绍了错误:agent.add()函数无法正常工作,即无法获取用户的输出值。在Google助手中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码正在工作。但是 agent.add()函数不起作用,即无法将输出值提供给用户。

Following code is working. But agent.add() function is not working, i.e not getting output value to user.

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()是不能正常工作,但是 console.log()在数据库日志中可以正常工作。

agent.add() is not working but, console.log() is working fine in database logs.

推荐答案

agent.add()确实有一些语法问题,但这不是问题的核心。

While agent.add() does have some syntax problems in it, that isn't the core of your problem.

每次循环时,您似乎也都获得了一个名为 FirstName的快照的子对象,我认为这不是您想要执行的操作,但这也不是核心问题。

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. But this also isn't the core of the problem.

问题是 query.once()是异步调用,而您使用回调函数来处理它。 Google行动库要求您返回一个Promise,以表明您正在进行异步函数调用,因此它知道在调用完成之前不会向用户返回任何内容。

The issue is that query.once() is an asynchronous call, and you're using a callback function to handle it. 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.

执行此操作的最佳方法是让 query.once()返回一个Promise,在 .then()中处理您的处理。 / code> Promise的一部分,并返回处理程序中的Promise / then链。

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.

我还没有测试过,但是看起来像这样:

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

这篇关于错误:agent.add()函数无法正常工作,即无法获取用户的输出值。在Google助手中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆