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

你说的曾经没有我的故事 提交于 2019-12-08 06:48:06

问题


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() is not working but, console.log() is working fine in database logs.


回答1:


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

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.

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.

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



回答2:


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.



来源:https://stackoverflow.com/questions/57678290/error-agent-add-function-is-not-working-i-e-not-getting-output-value-to-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!