Firebase的云端功能 - 移除最老的孩子 [英] Cloud Functions for Firebase - Remove oldest child

查看:114
本文介绍了Firebase的云端功能 - 移除最老的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个onWrite云端函数,用来侦听用户何时更新的内容。我试图删除最大的孩子,如果有超过3,这是我在:

 出口。 removeOld = functions.database.ref('/ users / {uid} / media')。onWrite(event => {

const uid = event.params.uid
$ b $ (event.data.numChildren()> 3){
//移除最老的孩子...
}

})

这些子元素中的每一个都有一个timestamp键。 b
$ b

  {
users:{
jKAWX7v9dSOsJtatyHHXPQ3MO193:{
media:{
-Kq2_NvqCXCg_ogVRvA:{
date:1.501151203274347E9,
title:Something ...
},
-Kq2_V3t_kws3vlAt6B:{
date:1.501151232526373E9,
title:Hello World ..
}
-Kq2_V3t_kws3B6B:{
date:1.501151232526373E9,
title:Hello World ..
}
}





所以在上面的例子中,当文本值被添加到媒体时,最旧的将被删除。 孩子rel =nofollow noreferrer>这个例子应该会对你有所帮助。



你需要类似的东西:

  const MAX_LOG_COUNT = 3; 

exports.removeOld = functions.database.ref('/ users / {uid} / media / {mediaId}')onCreate(event => {
const parentRef = event。 (snapshot.numChildren()> = MAX_LOG_COUNT){$ b($) $ b $ let childCount = 0;

const updates = {};

snapshot.forEach(function(child){
if(++ childCount< = snapshot.numChildren() - MAX_LOG_COUNT){
updates [child.key] = null;
}
});

//更新父项。
return parentRef.update(updates);
}
});
});

您可以在这里找到Firebase样本的所有Cloud Functions。


I have a onWrite cloud function set up to listen for when a user updates something. I'm trying to delete the oldest child if there are more than 3, this is there I'm at:

exports.removeOld = functions.database.ref('/users/{uid}/media').onWrite(event => {

    const uid = event.params.uid

    if(event.data.numChildren() > 3) {
        //Remove Oldest child...
    }

})

Each of these children has a "timestamp" key.

{
  "users" : {
    "jKAWX7v9dSOsJtatyHHXPQ3MO193" : {
      "media" : {
        "-Kq2_NvqCXCg_ogVRvA" : {
          "date" : 1.501151203274347E9,
          "title" : "Something..."
        },
        "-Kq2_V3t_kws3vlAt6B" : {
          "date" : 1.501151232526373E9,
          "title" : "Hello World.."
        }
        "-Kq2_V3t_kws3B6B" : {
          "date" : 1.501151232526373E9,
          "title" : "Hello World.."
        }
      }
    }
  }
}

So in the above example, when the text value is added to "media", the oldest would be delete.

解决方案

This sample should help you.

You need something like that :

const MAX_LOG_COUNT = 3;

exports.removeOld = functions.database.ref('/users/{uid}/media/{mediaId}').onCreate(event => {
    const parentRef = event.data.ref.parent;

    return parentRef.once('value').then(snapshot => {
        if (snapshot.numChildren() >= MAX_LOG_COUNT) {
            let childCount = 0;

            const updates = {};

            snapshot.forEach(function(child) {
                if (++childCount <= snapshot.numChildren() - MAX_LOG_COUNT) {
                    updates[child.key] = null;
                }
            });

            // Update the parent. This effectively removes the extra children.
            return parentRef.update(updates);
        }
    });
});

You can find all Cloud Functions for Firebase samples here.

这篇关于Firebase的云端功能 - 移除最老的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆