【问题标题】:Add parent child whenever onchild created cloud function real time database每当onchild创建云功能实时数据库时添加父子
【发布时间】:2018-10-06 15:31:26
【问题描述】:

这里我有代码触发 ref /tugas_course/{course_id}/{tugas_id} 每当添加孩子时都会向 android 设备发送通知。它已经做得很好了。但我想在其中再添加一个函数,我想在这个名为flag_tugas 的引用之外添加子元素,并将填充flag_tugas-course_id-tugas_id-user.uid: "o"。我不知道怎么加,因为返回值已经全部取走了,怎么在云函数中获取用户id。

export const onNotifTugas = functions.database.ref('/tugas_course/{course_id}/{tugas_id}')
.onCreate((snapshot, context) =>{


    const course_id = context.params.course_id;
    const tugas_id = context.params.tugas_id;

    console.log(`New Message ${course_id} in ${tugas_id}`);

    return admin.database().ref('/tugas/' + tugas_id +'/').once('value').then(snap => {
            const tugasData = snap.val();
            const notifDataSend = { // buat structure data json dgn nama const notifDataSend untul cloud messaging
                data: {
                            data_type: "tugas",
                            title: "Anda mendapatkan notifikasi baru..", // data bebas (key, value)
                            body: `Tugas ${tugasData.nama_tugas} baru`, // chatId = const chatId
                            sound: "default"
                      }
             };

            console.log(`data yang dikirim `);
            return admin.messaging().sendToTopic(course_id, notifDataSend) 

            .then(function(response) {
                console.log("Successfully sent message:", response);
              })
            .catch(function(error) {
                console.log("Error sending message:", error);
              });

    }).catch(error => {
        console.log(error);
    })

});


    }

实际上是在eclassapp下添加新的child

非常感谢您的时间和回答..

【问题讨论】:

    标签: node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    我不确定您要在节点树中的何处添加时间戳(在 /tugas_course/{course_id}/{tugas_id}/new_child/tugas_course/{course_id}/{tugas_id} 下),但以下代码可用于在 /tugas_course/{course_id}/{tugas_id} 下添加 timestamp 值.如果您需要,您可以更改 ref 的值以将时间戳写入您想要的位置。

    exports.onNotifTugas = functions.database
      .ref('/tugas_course/{course_id}/{tugas_id}')
      .onCreate((snapshot, context) => {
        const ref = snapshot.ref;
        const ts = admin.database.ServerValue.TIMESTAMP;
        return ref.update({
          date_time: ts
        });
      });
    

    但是请注意,您不需要 Cloud Function 来从服务器添加时间戳,请参阅 https://firebase.google.com/docs/reference/js/firebase.database.ServerValue

    所以在你的情况下,你会做这样的事情:

    var tugaRef = firebase.database().ref("/tugas_course/course_id/tugas_id");
    tugaRef.push({
      foo: bar,  //Other data of your 'tugas' object
      createdAt: firebase.database.ServerValue.TIMESTAMP
    })
    

    【讨论】:

    • 我只想在那个 ref 中创建孩子时,所以它会再添加一个孩子,这是创建帖子的时间。有关具体信息,我想将孩子称为“date_time”并包含“Tue Apr 23 16:08:28 GMT+05:30 2013”​​之类的值
    • 只需调整答案中的ref 值即可满足您的需求。我已将 createdAt 字段重命名为 date_time。时间戳值与您要查找的格式相对应。
    • 先生,现在我意识到我必须像这样添加孩子。我已经更新了我的代码
    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多