我希望实时数据库中有一个整数值与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对象,不确定如何访问计量表字段。
答案 0 :(得分:0)
为此,您将需要使用云功能,假设您有一个后台功能:
export const one = functions.firestore.document('users').onWrite((change, context) => {
// ... Your code here
})
export const two = functions.database.ref('/users/{userId}')
.onCreate((snapshot, context) => {
// ... you code here
})
由于您可以在那里访问firebase sdk管理员,因此基本上可以实现您的目标。您可以详细了解here
答案 1 :(得分:0)
这样做
jdbcTemplate.execute()
您实际上定义了DocumentReference
。
您必须使用set()
或update()
方法将此var userDoc = admin.firestore().doc(`user/${context.params.user1}`);
写入Firestore数据库。
以下是使用DocumentReference
方法的代码:
set()