【发布时间】:2019-03-29 06:12:51
【问题描述】:
我正在对 google 项目执行一项操作,需要将数据添加到两个不同的 Cloud Firestore。出于某种原因,当我触发意图时,它只会保存到原来的 Cloud Firestore,而不会保存到新的。
为简单起见,我将原始 Cloud Firestore 称为“DB1”,将新的一个/秒称为“DB2”
这是我尝试过的:
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
const {google} = require('googleapis');
const {
<Basically All of the libraries / AOG functions>
} = require('actions-on-google');
const defaultAppConfig = {"<FIREBASE CREDENTIALS FOR DB1 >"}
const OtherAppConfig = {"<FIREBASE CREDENTIALS FOR DB2>"}
const defaultApp = admin.initializeApp(defaultAppConfig); // DB1
const otherApp = admin.initializeApp(OtherappConfig, 'Other'); // DB2
const db = admin.firestore(functions.config(defaultApp).firebase); //DB1
const ab = admin.firestore(functions.config(otherApp).firebase); // DB2
const app = dialogflow({
debug: true,
clientId: '<DIALOGFLOW CREDENTIALS>'
});
app.intent('DB2 Write', (conv) =>{
conv.ask('Okay I made the write to DB2');
var data = {
name: 'This is a Test write'
};
var setDoc = ab.collection('Test').doc('Write').set(data);
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
抱歉,如果某些部分是不必要的,我想尽可能多地包含信息(我可能会遗漏其他人看到的内容)。
总结一下我认为会发生的事情,我认为当我触发“DB2 写入”意图时,它会将“这是一个测试写入”写入 DB2,但它只是继续将消息/数据写入 DB1。
我如何让它工作,以便在触发此意图时它会写入我的第二个 Cloud Firestore 或“DB2”?
感谢您的帮助!
注意:如果有影响,我将使用 dialogflow 内联编辑器来编写此代码。
_______________________________________________________________
更新:这是我尝试/更新的内容,它仍然写入 DB1
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
const otherAdmin = require('firebase-admin');
otherAdmin.initializeApp({
credential: otherAdmin.credential.cert(OtherAppConfig)
},'Other');
const ab = otherAdmin.firestore();
还有:
admin.initializeApp(defaultAppConfig);
var otherApp = admin.initializeApp(OtherAppConfig, 'other');
console.log(admin.app().name); // '[DEFAULT]'
console.log(otherApp.name); // 'other'
// Use the shorthand notation to retrieve the default app's services
var db = admin.firestore(functions.config().firebase);
// Use the otherApp variable to retrieve the other app's services
var ab = otherApp.firestore(functions.config().firebase);
我想指出,我用于“OtherAppConfig”和“defaultAppConfig”的凭据来自 Firebase 私钥。即:firebase 控制台 > 项目概述 > 服务帐户 > 生成私钥。会不会是这个问题?
【问题讨论】:
-
我看到您正在使用
ab编写文档。您也没有使用db来做同样的事情。此外,您似乎没有处理setDoc中返回的承诺。几乎可以肯定你必须为此做点什么。 -
抱歉澄清一下,我有其他使用 'db' 的意图,但当我使用 'ab' 或 DB2 时,它会写入 'db' 或 DB1
-
firebase中有2种实时数据库,云firestore和实时数据库,你的是什么?
-
云火库,我来更新
-
这是两个不同的项目吗?
标签: node.js firebase google-cloud-firestore dialogflow-es actions-on-google