Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript firebase firestore函数未执行_Javascript_Node.js_Firebase_Actions On Google_Google Cloud Firestore - Fatal编程技术网

Javascript firebase firestore函数未执行

Javascript firebase firestore函数未执行,javascript,node.js,firebase,actions-on-google,google-cloud-firestore,Javascript,Node.js,Firebase,Actions On Google,Google Cloud Firestore,我尝试用ActionsSDK、Firebase函数和Firebase Firestore构建一个ActionsOnGoogle应用程序 我建立了一个有目的的满足感。启动操作应用程序时的mainIntent和用户回答mainIntent问题时的answerIntent。从firestore数据库获取mainIntent中的文本,并要求具有包装文本的用户工作得非常好。但是,在从数据库数组包装新数据的函数中,会填充新数据。执行此函数后,数组将为空 这是我的代码: function answer

我尝试用ActionsSDK、Firebase函数和Firebase Firestore构建一个ActionsOnGoogle应用程序

我建立了一个有目的的满足感。启动操作应用程序时的mainIntent和用户回答mainIntent问题时的answerIntent。从firestore数据库获取mainIntent中的文本,并要求具有包装文本的用户工作得非常好。但是,在从数据库数组包装新数据的函数中,会填充新数据。执行此函数后,数组将为空

这是我的代码:

    function answerIntent(app){
    console.log('DEBUG: answerIntent acsess');
    console.log('DEBUG: ' + partAwnserRef);

    var rawInput = app.getRawInput();

    var answerCollection = db.collection(partAwnserRef);
    var answers = answerCollection.get().then(collection => {
      console.log('DEBUG: Collection size: ' + collection.size);
      collection.forEach(document => {
        if(document.exists){
          console.log('DEBUG: document ID: ' + document.id + ' = ' + document.data());
          answerDocumentArray.push(document);
          console.log('DEBUG: ArraySize: ' + answerDocumentArray.length);
          //HERE MY ARRAY WAS FILLED CORRECTLY
        }
        else{
          console.log('DEBUG: Dokument existiert nicht;');
          app.tell('Es liegt ein Fehler vor!');
        }
      });
    })
    .catch(err => {
      console.log('DEBUG: Es ist ein Fehler aufgetretten ' + err); 
      app.tell('Es liegt ein Fehler vor!');
    });

    //HERE THE OUTPOT IN THE LOG SAY THE ARRAY IS EMPTY
    console.log("DEBUG: lenght " + answerDocumentArray.length);

    for(var i = 0; i < answerDocumentArray.length; i++){
      var propertyNameArray = [];

      for(var propertyName in answerDocumentArray[i]){
        propertyNameArray.push(propertyName);
        console.log('DEBUG: propertyName: ' + propertyName);
      }

      for(var j = 0; j < propertyNameArray.length; j++){
        if(propertyNameArray[j].includes('answer')){
          if(rawInput.includes(answerDocumentArray[i].propertyNameArray[j])){
            app.tell(answerDocumentArray[i].propertyNameArray[j]);
          }
          else{
            var inputPrompt = app.buildInputPrompt(false, 'Ich habe die leider nicht verstanden!', ['Kannst du deine Antwort wiederholen?']);
            app.ask(inputPrompt); 
          }
        }
      }
    }
   };
function answerIntent(应用程序){
log('DEBUG:answerIntent acsess');
log('DEBUG:'+partAwnserRef);
var rawInput=app.getRawInput();
var answerCollection=db.collection(partAwnserRef);
var answers=answerCollection.get()。然后(collection=>{
log('DEBUG:Collection size:'+Collection.size);
collection.forEach(文档=>{
if(document.exists){
console.log('DEBUG:documentid:'+document.ID+'='+document.data());
应答文件推送(文件);
log('DEBUG:ArraySize:'+answerDocumentArray.length);
//这里我的数组被正确填充了
}
否则{
log('DEBUG:Dokument existiert nicht;');
app.tell('Es liegt ein Fehler vor!');
}
});
})
.catch(错误=>{
log('DEBUG:Es ist ein Fehler aufgetreten'+err);
app.tell('Es liegt ein Fehler vor!');
});
//这里,日志中的输出表示数组为空
日志(“调试:长度”+answerDocumentArray.length);
对于(变量i=0;i
当我查看firebase日志时,可以看到我的自编码日志。在这里我得到了以下信息。第一个日志是最新的

调试:长度0

调试:集合大小:2

调试:文档ID:answer1=[对象]

调试:阵列化:1

调试:文档ID:answer2=[对象]

调试:阵列化:2


您有两个问题,这两个问题基本上都涉及到理解如何从您正在进行的工作中返回结果:

问题1:履行承诺

原因
answerDocumentArray
DEBUG:lengh
行中为空是因为调用
answerCollection.get()
实际上并没有返回值。它返回一个JavaScript Promise对象,该对象最终将解析为值

但是,在执行之前,它将继续执行代码,代码中的下一行是调试行

当解析这些值时,将调用
then()
子句中的函数,并开始处理返回的集合。但这可能会在调试行后面的行之后执行。当函数完成时,它应该返回另一个Promise,其中包含指示需要进一步评估的内容的任何结果。(在本例中-您没有返回任何结果为空的内容。)

您可能需要做的是使用另一个
then()
子句,将传递给
answerDocumentArray
的第一个子句链接起来并使用它。大致如下:

var answers = answerCollection.get().then( collection =>{
    console.log( 'DEBUG: Collection size: ' + collection.size );
    collection.forEach( document =>{
      if( document.exists
      ){
        console.log( 'DEBUG: document ID: ' + document.id + ' = ' + document.data() );
        answerDocumentArray.push( document );
        console.log( 'DEBUG: ArraySize: ' + answerDocumentArray.length );
        //HERE MY ARRAY WAS FILLED CORRECTLY
      }
      else{
        console.log( 'DEBUG: Dokument existiert nicht;' );
        app.tell( 'Es liegt ein Fehler vor!' );
      }
    } )
    return Promise.resolve( answerDocumentArray );
    ;
  } )
  .then( answers => {
    for( var i = 0; i < answers.length; i++ ){
      // ... Do stuff with the answers here
    }
  })
  .catch( err =>{
    console.log( 'DEBUG: Es ist ein Fehler aufgetretten ' + err );
    app.tell( 'Es liegt ein Fehler vor!' );
  } )
  ;
然而,谷歌的行动并非如此。每次调用意图时,只能向用户发送一个响应。该响应必须是单个
ask()
或单个
tell()
。你不能给他们打多次电话。如果您多次尝试调用它,结果是未定义的,但充其量只能得到第一个结果。(最坏情况下,它会崩溃。)


如果需要返回多个结果,则需要在循环过程中收集这些结果,然后仅根据收集的结果说些什么。

您有两个问题,这两个问题基本上都涉及到理解结果如何从您的工作中传递回来:

问题1:履行承诺

原因
answerDocumentArray
DEBUG:lengh
行中为空是因为调用
answerCollection.get()
实际上并没有返回值。它返回一个JavaScript Promise对象,该对象最终将解析为值

但是,在执行之前,它将继续执行代码,代码中的下一行是调试行

当解析这些值时,将调用
then()
子句中的函数,并开始处理返回的集合。但这可能会在调试行后面的行之后执行。当函数完成时,它应该返回另一个Promise,其中包含指示需要进一步评估的内容的任何结果。(在本例中-您没有返回任何结果为空的内容。)

您可能需要做的是使用另一个
then()
子句,将传递给
answerDocumentArray
的第一个子句链接起来并使用它。大致如下:

var answers = answerCollection.get().then( collection =>{
    console.log( 'DEBUG: Collection size: ' + collection.size );
    collection.forEach( document =>{
      if( document.exists
      ){
        console.log( 'DEBUG: document ID: ' + document.id + ' = ' + document.data() );
        answerDocumentArray.push( document );
        console.log( 'DEBUG: ArraySize: ' + answerDocumentArray.length );
        //HERE MY ARRAY WAS FILLED CORRECTLY
      }
      else{
        console.log( 'DEBUG: Dokument existiert nicht;' );
        app.tell( 'Es liegt ein Fehler vor!' );
      }
    } )
    return Promise.resolve( answerDocumentArray );
    ;
  } )
  .then( answers => {
    for( var i = 0; i < answers.length; i++ ){
      // ... Do stuff with the answers here
    }
  })
  .catch( err =>{
    console.log( 'DEBUG: Es ist ein Fehler aufgetretten ' + err );
    app.tell( 'Es liegt ein Fehler vor!' );
  } )
  ;
然而,谷歌的行动并非如此。对于每次您的意图,您只能向用户发送一个响应