サイズカウントするように、私はcount increase/decreaseを達成しようとしています:Firebaseクラウド機能は、子どもたちが<a href="https://firebase.google.com/docs/reference/functions/functions.database.DeltaSnapshot#val" rel="nofollow noreferrer">firebase cloud functions API reference</a>後
Uploads/
- Posts/
- post_1
- post_2
...
- Likes/
- post_1/
- Number: 4
- post_2/
- Number: 2
...
そして、場所以外
exports.LikeCount= functions.database.ref('/Posts/{postID}').onWrite(event => {
const Ref = event.data.ref;
const postID = event.params.postID;
const likeCount= Ref.parent.parent.child('/Likes/' + postID + '/Number');
return likeCount.transaction(current => {
if (event.data.exists() && !event.data.previous.exists()) {
return (current || 0) + 1;
}else if (!event.data.exists() && event.data.previous.exists()) {
return (current || 0) - 1;
}
}).then(() => {
console.log("Done");
});
});
を、それが与えられた例と同じです。
another exampleまた、好き嫌いの数が削除されると、好き嫌い(子供)の数が再計算されます。
ここに私のバージョン(または少なくともそれのアイデア)があり、好きな数をチェックし、1より小さい場合は再計算します。 (あなたの好きな数が存在しない場合、存在する数に関係なく、最初の関数が1を与えるからです)。第二の機能で
exports.reCount= functions.database.ref('/Likes/{postID}/Number').onUpdate(event => {
const value = event.data.val;
//If the value is less than 1:
if (value <= 1) {
const currentRef = event.data.ref;
const postID = event.params.postID;
const postRef = currentRef.parent.parent.child('/Uploads/Posts/{postID}/');
return postRef.once('value')
.then(likeData=> currentRef.set(likeData.numChildren()));
}
});
、私は文字列値を取得するだろうと思ったFBログに[Function: val]
を与え、次の場所event.data.val
を使用してNumber
値を取得しようとしました。
...およびcurrentRef.parent.parent.child('/Uploads/Posts/{postID}/').numChilren();
は、TypeError: collectionRef.numChildren is not a function
です。
オンラインチュートリアルやAPIリファレンスを読みましたが、なぜ文字列値を取得できないのか混乱しています。
私は私が働くことができるいくつかの例を探していますね。
[Firebaseのためのクラウドファンクション:インクリメントカウンタ](0120-184-3112) –
私はこれを読んだ。私が尋ねた質問は実際にリンクでは答えられていません。 –
"期待どおりに動かない"以外の観察はありますか? –