i try to build a ActionsOnGoogle App with the ActionsSDK, Firebase Functions and Firebase Firestore.
I build a fulfillment that haves to intents. The mainIntent when starting the Actions App and the answerIntent when the users answers the question from the mainIntent. Getting the text in the mainIntent from the firestore database and ask the users with the wrapped text work very fine. But then inside the function where new data is wrapped from the database array get filled. After executing this function the array ist empty.
This is my code:
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);
}
}
}
}
};
When i look inside the firebase logs is see my self coded logs. And there i get the following. First log ist latest.
DEBUG: lenght 0
DEBUG: Collection size: 2
DEBUG: document ID: answer1 = [object Object]
DEBUG: ArraySize: 1
DEBUG: document ID: answer2 = [object Object]
DEBUG: ArraySize: 2