【问题标题】:Within a firebase project, can I update values in the firestore database from the real time database在firebase项目中,我可以从实时数据库更新firestore数据库中的值吗
【发布时间】: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


    【解决方案1】:

    通过做

    var userDoc = admin.firestore().doc(`user/${context.params.user1}`);
    

    你实际上定义了一个DocumentReference。

    您必须使用 DocumentReference 写入 Firestore 数据库,使用 set() 或 update() 方法。

    这是使用set()方法的代码:

    exports.meterSync = functions.database
      .ref('/{userId}/meter')
      .onUpdate((change, context) => {
    
        const meterReading = change.after.val();
        console.log(meterReading);
    
        // Get a reference to the Firestore document of the changed user
        const userDoc = admin.firestore().doc(`user/${context.params.userId}`);
    
        return userDoc.set(
          {
            meter: meterReading
          },
          { merge: true }
        );
      });
    

    【讨论】:

    • 谢谢,雷诺!这有助于在 firestore db 中获取更新,但它会在 user 集合中创建一个新的 undefined 文档,而不是更新 userID 文档。你知道可能缺少什么吗?
    • “未定义的文档”到底是什么意思?您在 Firestore 端的确切数据结构是什么,您在实时数据库端更新的确切记录/节点是什么?您可以根据您的确切要求和数据结构微调代码。
    • ||||| Cloud Firestore:集合: 用户 -> 文档: 用户ID -> 字段: 抄表 |||||实时数据库:userID / meterReading |||||当我使用上面的代码示例时,它没有更新相同的云 Firestore Document : userID ,而是创建了一个新的 Document : undefined
    • 不应该是|||||实时数据库:用户ID/米|||||就像你的问题一样?如果您得到一个未定义的文档 ID,那是因为 context.params.userId 未定义。您应该通过 console.logs 在您的环境中调试代码,并可能对其进行调整。
    • 是的,我打字的时候错了,是|||||实时数据库:用户ID/米||||| ...我会调试,但你的意思是 context.params.userId 是 undefined 吗?那不是指向具体的文件吗?
    【解决方案2】:

    您需要为此使用云功能,假设您有一个后台功能:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多