如何做到这一点Firebase云功能我无法使用它

时间:2017-11-16 19:46:24

标签: android firebase firebase-realtime-database google-cloud-functions google-cloud-firestore

在Cloud Firestore中,我尝试创建一个Function触发器,但是我遇到了一些问题。

我希望Cloud Firestore功能响应此Firebase Realtime数据库位置的写入:

"VISITORS_PRESENCE" : {
    "8PICNP6JBTJDFGiBZYsT" : { // House for sale
      "3b1874ec-02f8-4004-817d-a3c44877473c" : { // The user Id
        "applicationId" : "3b1874ec-02f8-4004-817d-a3c44877473c",
        "last_changed" : 1510859721293,
        "state" : "online",
        "userId" : "testtest@test,com"
      }
    }
  },

并简单地将此值/写入复制到Cloud Firestore使用此功能: (请注意,这是基于Cloud Firestore docs functions get-started

exports.onvisitorPresence = functions.database
    .ref("/VISITORS_PRESENCE/{uid1}/{uid2}").onWrite((event) => {
        // Get the data written to Realtime Database
        const eventStatus = event.data.val();

        // Then use other event data to create a reference to the
        // corresponding Firestore document.
        const userStatusFirestoreRef = firestore
        .doc(`VISITORS_PRESENCE/${event.params.uid1}/USERS/{event.params.uid2}`);

        // It is likely that the Realtime Database change that triggered
        // this event has already been overwritten by a fast change in
        // online / offline status, so we'll re-read the current data
        // and compare the timestamps.
        return event.data.ref.once("value").then((statusSnapshot) => {
            return statusSnapshot.val();
        }).then((status) => {
            console.log(status, eventStatus);
            // If the current timestamp for this data is newer than
            // the data that triggered this event, we exit this function.
            if (status.last_changed > eventStatus.last_changed) return;

            // Otherwise, we convert the last_changed field to a Date
            eventStatus.last_changed = new Date(eventStatus.last_changed);

            // ... and write it to Firestore.
            return userStatusFirestoreRef.set(eventStatus);
        });
    });

在Cloud Firestore中,它产生的是:

VISITORS_PRESENCE / 8PICNP6JBTJDFGiBZYsT /用户/ {event.params.uid2} /
applicationId“3b1874ec-02f8-4004-817d-a3c44877473c”
last_changed 2017年11月16日下午8:15:21 UTC + 1
国家“在线”
userId“testtest @ test,com”

这是一张照片: enter image description here

我希望上面的event.params.uid2是“3b1874ec-02f8-4004-817d-a3c44877473c”,这是userId。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您的功能由对Firebase实时数据库的写入操作触发:

exports.onvisitorPresence = functions.database
    .ref("/VISITORS_PRESENCE/{uid1}/{uid2}").onWrite((event) => {

要通过写入Cloud Firestore来触发功能,请使用:

exports.myFunctionName = functions.firestore
  .document('users/marie').onWrite((event) => {

有关更长的示例,请查看documentation for triggering Cloud Functions from Firebase

答案 1 :(得分:0)

我做了一个错误的错误。我忘记了$ sign

  

.DOC(VISITORS_PRESENCE/${event.params.uid1}/USERS/{event.params.uid2});

     

.DOC(VISITORS_PRESENCE/${event.params.uid1}/USERS/${event.params.uid2});

现在Cloud Firestore数据看起来还不错:

VISITORS_PRESENCE / 8PICNP6JBTJDFGiBZYsT /用户/ 8PICNP6JBTJDFGiBZYsT /
applicationId" 3b1874ec-02f8-4004-817d-a3c44877473c"
last_changed 2017年11月16日下午8:15:21 UTC + 1
国家"在线"
userId" testtest @ test,com"

相关问题