agent.add не работает, но console.log работает - PullRequest
2 голосов
/ 18 октября 2019

В приведенном ниже коде «agent.add» не работает, но работает «console.log». Я добавил обещание с решимостью и отказом, но все равно не работает. Я пробовал разные способы, но несколько ответов от Firestore, не удалось отправить его пользователю. Возможность видеть журналы в FireBase, но не в диалоговом потоке.

    const {Firestore} = require('@google-cloud/firestore');
    const functions = require('firebase-functions');
    const {WebhookClient} = require('dialogflow-fulfillment');
    const {Card, Suggestion} = require('dialogflow-fulfillment');
    const admin = require('firebase-admin');

    admin.initializeApp();

    process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging 
     statements
        const firestore = new Firestore();
        const settings = {/* your settings... */ 
                          timestampsInSnapshots: true};
    firestore.settings(settings);

    exports.dialogflowFirebaseFulfillment = 
     functions.https.onRequest((request, response) => {
      const agent = new WebhookClient({ request, response });
      console.log('Dialogflow Request headers: ' + 
     JSON.stringify(request.headers));
      console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
     const db = admin.firestore();

    ```
    function welcome(agent) {
      agent.add(`Welcome to my agent!`);  // not working
      console.log("Welcome Agent");
      const num = agent.parameters.number;

      let usersRef = db.collection('add');
      let query = usersRef.where('Number', '==', num);
      return new Promise((resolve,reject)=>{   // not working
        return query.get()
          .then(querySnapshot => {
            if (querySnapshot.empty) {/*
            const timestamp = querySnapshot.get('created_at');
            const date = timestamp.toDate();*/
              console.log('No matching documents.');
              agent.add(`No Matching Documents`);
              return;
            }
            querySnapshot.forEach(doc => {
              const line1 = doc.get('Line1');
              const line2 = doc.get('Line2');
              const explain = doc.get('explanation');
              console.log('Line1: ', line1);  //this works
              console.log('Line2: ', line2);  //this works
              console.log('explain: ', explain); //this works
              agent.add(`your response is ` +doc.get('Line1')); //not working
              agent.add(`Final Response -  ${line2}`); //not working
              agent.add(doc.get('explanation')); //not working
            });
            resolve('resolved');
          })
          .catch(err => {
            console.log('Error getting documents', err);
            reject(err);
          });
      });
    }

1 Ответ

0 голосов
/ 22 октября 2019

Проблема решена. Добавлен оператор return в последнем agent.add, и он работает. Благодарю.

agent.add(your response is +doc.get('Line1')); 
agent.add(Final Response - ${line2}); 
return agent.add(doc.get('explanation')); 
...