简体   繁体   English

来自2个节点的Firebase查询

[英]Firebase query from 2 nodes

cards

    cars
    -- idcars
        -- type:
        -- age:
        -- user: iduser

    users
    -- iduser
     -- username
     -- usermail


    ads
    -- iduser
     -- ads1 : idcars
     -- ads2 : idcars

With this structure how it's possible to retrieve all ads for a user together with their own data? 通过这种结构,如何能够为用户检索所有广告以及他们自己的数据?

Example: 例:

ads1: 
type: golf
age:2

I'm completely new with Firebase 我对Firebase完全陌生

This should be what you need to get the authenticated user and then you can use this model to just retrieve the whole ads branch filtering through it to have everyones ads detail (if you want) 这应该是获取经过身份验证的用户所需要的,然后可以使用此模型来检索通过它过滤的整个ads分支,以使每个人的广告详细信息(如果需要)

let userID = FIRAuth.auth()?.currentUser?.uid
ref.child("ads").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in

  // Get user value
  let value = snapshot.value as? NSDictionary // This gets the whole ad branch with all the users ad data
  let adsValue = value?["ads1"] as? String ?? "" // This is the specific value of 'ads1' node

  // ...
  }) { (error) in
    print(error.localizedDescription)
}

Maybe like this, 也许是这样

get the user id then put it in ref.child(yourString) 获取用户ID,然后将其放入ref.child(yourString)

ref = FIRDatabase.database().reference()
    ref.child("iduser").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in })

This will require two read operations, one for the user and one for their ads. 这将需要两项读取操作,一项针对用户,一项针对其广告。 See the Firebase documentation for reading data and for querying lists . 有关读取数据查询列表的 信息 ,请参阅Firebase 文档

If you're having trouble making this work, post the minimal code that reproduces the problem . 如果您在进行这项工作时遇到麻烦,请张贴重现该问题最少代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM