ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파이어스토어 해당 값이 있는지 없는지 확인
    파이어베이스 2021. 11. 5. 20:18

    좋아요 기능이나 기타 여러가지 검증작업시, 해당 값이 있나 없나 파악해야 할 때가 있습니다.

    다른 방법도 있겠지만..

    저같은경우 기본 콜렉션 밑에 서브 콜렉션으로 검증이 필요한 도큐먼트를 입력하게하여 구현했습니다.

     

     

     

    //게시글에 좋아요를 클릭한 사용자의 아이디값을 입력

    Future addPostLikeUser(String? postUid, String? userId, var ref) {
    return ref
    .collection('likeUser')
    .doc(userId)
    .set({'userId': authBloc.currentUserId, 'postUid': postUid});
    }

     

     

     

     

    //좋아요 한적 있는지 체크
    Future<bool> ifExistLike(String? postUid, String userId) {


    var isExists = (DocumentSnapshot doc) => doc.exists;
    l.info(this, 'ifExistLike userid >>> ' + userId);

    return Future.wait([
    _db
    .collection('posts')
    .doc(postUid)
    .collection('likeUser')
    .doc(userId)
    .get()
    .then(isExists),
    ]).then((xs) {
    l.info(this, 'Future like before bool then xs >> ' + xs.toString());
    // 존재함
    return xs.every((x) => x == true);
    });
    }

     

    true가 리턴되면 데이터값이 있음을 알려줍니다.

     

     

     

     

     

    '파이어베이스' 카테고리의 다른 글

    파이어베이스 도큐먼트 id 자동생성  (0) 2021.11.03
Designed by Tistory.