首页 > 解决方案 > 似乎无法将 Firestore 数据推送到 Dialogflow

问题描述

我在将一些数据从 Firestore 拉回 Dialogflow 时遇到了一些麻烦。我可以控制台记录该值,但它没有被推送到 agent.add()

我有一个产品集合,每个文档都包含产品信息:

产品 > firebaseId >

{
name: "Coca Cola",
price: 1.00
}

更新代码:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  
  function readFromDb (agent) {
    const prodName = agent.parameters.product;
    
    const dialogflowAgentDoc = db.collection('dialogflow').where('name', '==', prodName);
    

    return dialogflowAgentDoc.get()
      .then(doc => {
        if (!doc.exists) {
          agent.add('No data found in the database!');
        } else {
          agent.add('The price of ' + doc.data().name + ' is ' + doc.data().price + ' dollars!');
        }
        return Promise.resolve('Read complete');
      }).catch(() => {
        agent.add('Error reading entry from the Firestore database.');
        
      });
  }


  let intentMap = new Map();
  intentMap.set('priceCheck', readFromDb);
  agent.handleRequest(intentMap);
});

更新:类型错误不再出现

但是请求超时...我尝试对 RTDB 执行相同的操作,但似乎根本无法运行查询。

标签: firebasegoogle-cloud-firestoredialogflow-esdialogflow-es-fulfillment

解决方案


这里有一些问题,可能隐藏了真正的错误是什么。

为什么我会收到 TypeError?

因为.catch()试图在你的结果上被调用snapshot.forEach(),它不返回任何东西。它可能应该被移出一层——它是在 的结果上调用的product.then(),它是一个 Promise。

为什么调用了console.log(),却没有调用agent.add()?

我认为agent.add()正在调用,但是因为有一个 TypeError,所以调用了一个异常,所以有一个隐式的 Promise 拒绝。因为有一个 Promise 拒绝,所以 Dialogflow 处理程序实际上并没有添加任何回复。

agent.add()至少乍一看,您的调用确实正确。


推荐阅读