firebase - 似乎无法将 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 执行相同的操作,但似乎根本无法运行查询。
解决方案
这里有一些问题,可能隐藏了真正的错误是什么。
为什么我会收到 TypeError?
因为.catch()
试图在你的结果上被调用snapshot.forEach()
,它不返回任何东西。它可能应该被移出一层——它是在 的结果上调用的product.then()
,它是一个 Promise。
为什么调用了console.log(),却没有调用agent.add()?
我认为agent.add()
正在调用,但是因为有一个 TypeError,所以调用了一个异常,所以有一个隐式的 Promise 拒绝。因为有一个 Promise 拒绝,所以 Dialogflow 处理程序实际上并没有添加任何回复。
agent.add()
至少乍一看,您的调用确实正确。
推荐阅读
- r - flexsurv 包:用于绘制两个研究组的 Kaplan-Meier 曲线预测的代码
- java - 如何使用 Java 在 Android Studio 中使用 dataType(字符串、整数、布尔值)创建枚举类型?
- typescript - Typescript ReferenceError: myApp 未定义
- dm-script - TagGroupGetTagType 返回错误的类型
- sql - SQL IN AND NOT IN 同一个请求
- python - Python中的列表和基于具有重复元素的索引的排序
- python - GCC 编译器版本与 Python 2.7.17 冲突
- java - 'android.annotation.NonNull' 在 'android.annotation' 中不公开。无法从外部包访问
- python - 如果键是python中的数字,有没有办法按键对字典进行排序?
- reactjs - 如何使用material-ui的网格系统将5个项目均匀地排列成一行?