【问题标题】:How can I get data from multiple fireStore collections in React native?如何从 React Native 中的多个 fireStore 集合中获取数据?
【发布时间】:2021-03-06 10:41:03
【问题描述】:

我有这些收藏:

  • 具有名称和图像的用户(Uid = 文档),
  • Post(Uid Post = doucment) 包括正文、图片和 Uid(Uid 用户)

我使用嵌套循环来拥有一个简单的对象(我不使用 2 个数组)

firebase.firestore().collection("Post").get()
            .then(querySnapshot => {
               querySnapshot.forEach(documentSnapshot => {
                    this.setState({
                        dataPost: this.state.dataPost.concat({
                            ...documentSnapshot.data(),
                            key: documentSnapshot.id,
                            name: this.getUserbyUid(documentSnapshot.data().uid) <- get Name user as soon as I get the uid from the Post collection
                        }),
                    })
              });
      });
  getUserbyUid = (Uid) => {
            firebase.firestore().collection('user').doc(Uid).get()
            .then(documentSnapshot => {
                return documentSnapshot.data().name
            })}

我收到一个错误name: Undefinded

这是我的想法,我知道有很多更好的方法请帮助我tks

发布

用户

Object {
  "body": "hfdbdb"
  "date": 1614903953253,
  "image": “"https://firebasestorage.googleapis.com/v0/t.....-44¢5-b091-7af310de36d1",
  "key": "IX9KKfcOONSyquUtossTd",
  "name": undefined,
  "uid": "Chc1P3zcWMguc2d2ppKSRrpQx752",
},

Object

【问题讨论】:

    标签: react-native google-cloud-firestore


    【解决方案1】:

    您需要首先检查您的数据结构,因为随着数据库的增长,循环和提取数据会变得非常复杂。

    您可以阅读更多关于不同的data structures here。每个都有不同的优点和缺点,选择一个将取决于您的用例。

    一旦你有一个良好的数据库基础,你的代码就会变得更干净,更容易阅读。

    以 Python 为例:

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore
    
    
    cred = credentials.ApplicationDefault()
    
    firebase_admin.initialize_app(cred, {
        'projectId': 'test-project',
    })
    
    db = firestore.client()
    

    获取用户数据

    users = db.collection('User').document('document')
    
    user = users.get()
    
    print(f'Data: {user.to_dict()}')
    
    . . .
    
    Data: {'avatar': 'http://my-avatar-image.com',
           'name': 'My Good Name'}
    

    获取帖子数据

    posts = db.collection('Post').document('document')
    
    post = posts.get()
     
    print(f'Data: {post.to_dict()}')
    
    . . .
    
    Data: {'date': '121445475589',
           'body': 'sdfsdfsdf',
           'uid': 'Hyhyfd987698IUHGKDSh8d',
           'image': 'http://my-example-image.com'
    }
    

    获取集合中的文档

    users = db.collection(u'User').stream()
    
    for user in users:
        print(f'{user.id} => {user.to_dict()}')
    
    . . . 
    
    document => {'avatar': 'http://my-avatar-image.com', 'name': 'My Name'}
    document1 => {'avatar': 'http://my-avatar-image.com', 'name': 'My Good Name'}
    
    

    从集合组中获取文档

    users = db.collection(u'Users')
    
    a_posts = users.document(u'A').collection(u'post')
    a_posts.document().set({
        u'name': u'My Good Name',
        u'type': u'user'
    })
    
    b_posts = users.document(u'B').collection(u'post')
    b_posts.document().set({
        u'name': u'My Best Name',
        u'type': u'user'
    })
    
    c_posts = users.document(u'C').collection(u'post')
    c_posts.document().set({
        u'name': u'My Real Name',
        u'type': u'user'
    })
    
    names = db.collection_group(u'post')\
        .where(u'type', u'==', u'user')
    
    all_users = names.stream()
    
    for user in all_users:
        print(f'{user.id} => {user.to_dict()}')
    
    

    可能的错误信息

    如果您创建数据库然后尝试直接提取数据,您可能会收到此错误消息。在后台建立索引时等待几分钟,然后重试。

    FailedPrecondition: 400 The query requires a COLLECTION_GROUP_ASC index for collection . . That index is not ready yet. 
    

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 2019-06-05
      • 1970-01-01
      • 2020-11-26
      • 2021-10-31
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      相关资源
      最近更新 更多