【问题标题】:Inner join collections based on locations with geofireX基于 geofireX 位置的内部连接集合
【发布时间】:2021-01-26 03:12:36
【问题描述】:

我想使用 geofirex 根据用户的位置获取并加入多个集合:

这是我根据位置获取用户文档时的代码: PS(此代码仅根据位置返回用户的文档)

getAround() {
    const center = geo.point(this.lat, this.lng);
    const field = 'location';
    this.points = this.radius.pipe(
      switchMap(r => {
        return this.geo.query(locations).within(center, r, field, { log: true });
      })
    );
}

我如何使用 Rxjs 内部加入集合:

 getAll() {
      const allPosts = this.afStore.collection<Service>('services')
        .valueChanges()
        .pipe(
          switchMap(services => {
            const res = services.map((service: Service) => {
              return this.afStore.doc<UserModel>(`users/${service.userId}`)
              .valueChanges()
              .pipe(
                map(user => Object.assign(service, { user }))
              );
            });
            return combineLatest([...res]); })
        )
         .pipe(
          switchMap(servuser => {
            const ress = servuser.map((service: Service) => {
              return this.afStore.doc<Category>(`categorys/${service.iconId}`)
              .valueChanges()
              .pipe(
                map(category => Object.assign(service, { category }))
              );
            });
            return combineLatest([... ress]);
          })
        );
      return allPosts;
    }

问题是我无法将这些代码混合在一起,因此我可以根据用户的位置加入我的数据,

我知道了error:

(alias) interface Service 
import Service 
Argument of type '(service: Service) => Observable<Service & { user: UserModel; }>' is not assignable to parameter of type '(value: GeoQueryDocument, index: number, array: GeoQueryOocument[]) => Observable<Service & { user: UserModel; }>'. Types of parameters 'service' and 'value' are incompatible. 

Type 'GeoQuerpocument' is missing the following properties from type 'Service': id, cost, dateCreated, discount, and 6 more. 

【问题讨论】:

    标签: angular firebase google-cloud-firestore rxjs geofire


    【解决方案1】:

    添加这个修复了它,现在我的代码可以工作了!

    .pipe(
              switchMap(services => {
                const res = services.map((fromservices) => {
                  return this.afStore.doc<UserModel>(`users/${fromservices['userId']}`)
                  .valueChanges()
                  .pipe(
                    map(user => Object.assign(fromservices, { user }))
                  );
                });
                return combineLatest([...res]); })
            )
    

    我猜只是有这个警告可以吗? :

    object access via string literals is disallowed (no-string-literal)tslint(1)
    

    【讨论】:

    • 看看this github thread:`它不鼓励你做 foo['bar']。与任何其他规则一样,它们是可选的,并且有人喜欢打开或关闭它。`
    【解决方案2】:

    错误表明 services 来自 value-index-array 类型,而您隐含地期望一个 Services 数组。

        .pipe(
          switchMap(services => { // <-- expects a Service array (but appreantly it's not)
            const res = services.map((service: Service) => { // <-- because of this
              return this.afStore.doc<UserModel>(`users/${service.userId}`)
              .valueChanges()
              .pipe(
                map(user => Object.assign(service, { user }))
              );
            });
            return combineLatest([...res]); })
        )
    

    【讨论】:

    • 感谢您的回答,您对如何将其他收藏品加入该地理查询有任何建议吗?我已经尝试了很多东西,但直到现在都没有成功。
    【解决方案3】:

    您可能想看看这个question。他们逐步解释了与您类似的场景。

    他们还分享了@98​​7654322@:使用上述代码和模拟 FireStore 的完整、可运行的演示。

    除此之外,我还发现了有关 Join Collections in Firestore 的内容,您可能会感兴趣。

    我建议看一下前面提到的问题。无论如何,我将在这里添加一些值得注意的东西:

    • 您可以使用pipeable transformation operators:
    const myQuery$ = this.myQuery.onSnapshot.pipe(
        map(querySnapshot => querySnapshot.docs.map(d => d.data()))
    );
    
    • 要加入这两个查询,combineLatest 是正确的创建函数。

    但是,您的错误可能是由于您使用了较新的 RxJS 版本,它不再支持 fluent 运算符(官方称为“补丁运算符”)。从 RxJS 6 开始,它们已被 pipeable operators 取代。例如,myObs$.map(...) 已变为 myObs$.pipe(map(...))。这些教程可能使用了旧版本的 RxJS,但仍然可以使用第一个版本。

    此外,如果内部 Observable 只是一个 of 运算符,则不必使用 switchMap。在这种情况下,使用 map 运算符就足够了,它的行为相同。

    将新的 RxJS 6+ 语法与 map 结合使用,组合将如下所示:

    const orQuery$ = combineLatest(myQuery$, publicQuery$).pipe(
        map(([one, two]) => one.concat(two))
    )
    

    旁注:请记住,SQL 中的代码等价物是 UNION(而不是 JOIN)。为了以编程方式加入,您需要将结果集 A 的每个对象与结果集 B 的每个对象组合起来,并为每对创建一个连接对象。这样一个无键 OUTER JOIN 的函数看起来像这样(放置在您的地图管道中):

    one.map(a => 
       two.map(b => Object.assign({}, a, b)))
    .reduce((p, c) => p.concat(c), [])
    

    如果您想要一个没有重复对象的 UNION,请仅连接列表一中没有匹配主键的两个项目。这将是您的映射函数:

    one.concat(two.filter(twoItem => !one.some(oneItem => oneItem.id == twoItem.id)))
    

    【讨论】:

      猜你喜欢
      • 2017-06-23
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      相关资源
      最近更新 更多