【发布时间】:2019-10-22 06:56:42
【问题描述】:
我的实时数据库中有一个整数值,我希望它与我的 firestore 数据库中的一个整数值同步。实时数据库通过外部源提供,当它获得更新时,我希望将其推送到 firestore 数据库
到目前为止,我可以访问实时数据库中的值,但不能访问 firestore 数据库中的值。
===============数据结构===================
实时数据库
user1:
{ meter : 20 }
Firestore database
Collection: Users
{Document : user1
{ meter : 20 }}
/// =============代码示例 ============================= =========
const functions = require('firebase-functions');
// Initialize the Firebase application with admin credentials
const admin = require('firebase-admin');
admin.initializeApp();
// Define user sync method
exports.meterSync = functions.database.ref('/user1/meter').onUpdate( (change, context) => {
// Get a reference to the Firestore document of the changed user
var userDoc = admin.firestore().doc(`user/${context.params.user1}`);
const meterReading = change.after.val();
console.log(meterReading);
console.log(userDoc); /// Not able to access this
return null
});
我的期望是用户文档会给我文档,所以我可以更新其中的字段。但我得到一个 documentReference 对象,不知道如何访问仪表字段。
【问题讨论】:
标签: firebase firebase-realtime-database google-cloud-firestore google-cloud-functions