의 크기를 계산하는, 내가 count increase/decrease을 달성하기 위해 노력하고 있어요 :중포 기지 클라우드 기능은 아이들이 <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()));
}
});
, 내가 사용
Number
값을 얻기 위해 노력 내가 문자열 값을 얻을 것이라고 생각 FB 로그에
[Function: val]
을 준 다음과 같은 경우
event.data.val
.
... 및 currentRef.parent.parent.child('/Uploads/Posts/{postID}/').numChilren();
은 TypeError: collectionRef.numChildren is not a function
입니다.
나는 온라인 튜토리얼과 API 레퍼런스를 읽었지만 여전히 문자열 값을 얻을 수없는 이유에 대해 혼란 스럽다.
나는 내가 할 수있는 몇 가지 예를 찾고있다.
[Firebase를위한 구름 기능 : 증가 카운터] (https://stackoverflow.com/questions/42914815/cloud-functions-for-firebase-increment-counter) –
나는 이것을 읽었습니다. 제가 물어 본 질문은 실제로 링크에서 대답하지 않습니다. –
"예상대로 작동하지 않습니다"이외의 관측이 있습니까? –