如何通过键中的参考值获取Firestore数据

问题描述 投票:1回答:2

enter image description here

这里是要约的多个文档,每个要约都包含引用了用户集合和用户ID的bidderId。

我想获取包含用户集合的商品列表。

我正在使用angularfire,这是我的代码。

this.liveOffers=this.db.collection("offers",ref => ref.where('offerExpired', '==', 0).where('isStart', '==', 1)).snapshotChanges().pipe(
map(actions => actions.map(a => {
  const data={} = a.payload.doc.data() as offer;
  const id = a.payload.doc.id;
  var bidder=this.db.doc(data.bidderId).snapshotChanges().subscribe(key=>{
    console.log(key.payload.data());
  });
  return { id, ...data,bidder };
}))   );

这里console.log(key.payload.data());正在为用户记录数据,但无法与bidder变量绑定,并且我无法在前端使用user对象。

请让我知道如何获取带有用户详细信息的报价记录。

firebase nosql google-cloud-firestore angularfire angular7
2个回答
0
投票

您需要使用switchMapcombineLatest的组合来完成它。

这是伪代码方法

const temp = []
this.offers$ = this.db.collection().snapshotChanges().pipe(
  map(auctions=>{
    //we save all auctions in temp and return just the bidderId 

    return auctions.map(auction=>{
      const data={} = a.payload.doc.data() as offer;
      const id = a.payload.doc.id;
      temp.push({id, ...data})
      return data.bidderId
    })


  }),
  switchMap(bidderIds=>{
    // here you'll have all bidderIds and you need to return the array to query 
    // them to firebase
    return combineLatest(bidderIds.map(bidderId=> return this.db.doc(bidderId)))
  }),
  map(bidders=>{
    // here you'll get all bisders you'll have to set the bidder on each temp obj 
    // you saved previously
  })
)

请确保您import { combineLatest } from 'rxjs/operators'不是'rxjs'


0
投票

我找到了一种方法。这是工作。但是我认为它有点大,可能有优化的方法。这也是node-js服务器API,不适用于Web(JS)。同样,对于Web(JS)可能会有类似的解决方案

一旦从快照对象获取数据,在数据返回的对象中就有_path键,该数据又具有segments,它是数组,并包含集合和ID

const gg = await firestore.collection('scrape').doc('x6F4nctCD').get();

console.log(JSON.stringify(gg.data(), null, 4));
console.log(gg.data().r._path.segments[0]);
console.log(gg.data().r._path.segments[1]);

const gg2 = await firestore
.collection(gg.data().r._path.segments[0])
.doc(gg.data().r._path.segments[1])
.get();

console.log(gg2.data());

{
    "fv": {
        "_seconds": 1578489994,
        "_nanoseconds": 497000000
    },
    "tsnowpo": 1578489992,
    "createdAt": {
        "_seconds": 1578489992,
        "_nanoseconds": 328000000
    },
    "r": {
        "_firestore": {
            "_settings": {
                "libName": "gccl",
                "libVersion": "3.1.0",
                "servicePath": "firestore.googleapis.com",
                "port": 443,
                "clientConfig": {},
                "scopes": [
                    "https://www.googleapis.com/auth/cloud-platform",
                    "https://www.googleapis.com/auth/datastore"
                ]
            },
            "_settingsFrozen": true,
            "_serializer": {},
            "_projectId": "sss",
            "_lastSuccessfulRequest": 1578511337407,
            "_preferTransactions": false,
            "_clientPool": {
                "concurrentOperationLimit": 100,
                "activeClients": {},
                "terminated": false
            }
        },
        "_path": {
            "segments": [
                "egpo",
                "TJTHMkxOx1C"
            ]
        }
    }
}
egpo
TJTHMkxOx1C
{ name: 'Homeware' }

© www.soinside.com 2019 - 2024. All rights reserved.