【问题标题】:GroupBy transformation operator is working only on local environmentGroupBy 转换运算符仅适用于本地环境
【发布时间】:2021-09-28 05:32:29
【问题描述】:

我正在尝试重构代码,同时我发现了一个我想了解的错误。 它适用于本地环境,但不适用于 QA 环境。 数据:

const dataSet: DataSetInterface[] = [
      { id:57, firstProperty:148, secondProperty:1, thirdProperty:10.30 },
      { id:58, firstProperty:149, secondProperty:4, thirdProperty:20.60 },
      { id:59, firstProperty:148, secondProperty:4, thirdProperty:22.20 }
    ];

旧方法不起作用:

private groupAndSumUp(dataSet: DataSetInterface[]) {
  Rx.Observable.from(dataSet)
      .groupBy(x => x.secondProperty)
      .flatMap(group => group.toArray())
      .map(g => {
        return {
          id: g[0].secondProperty,
          secondProperty: g[0].thirdProperty,
          budgetDays: _.sumBy(g, 'budgetDays'), 
        }
      })
      .toArray()    
      .subscribe(d => this.budgetDays = d); 
}

在本地它正在工作,但在 QA 环境中我收到错误:

ERROR TypeError: e.map is not a function

我不知道为什么。可能是因为 GroupedObservables 在第一个方法 toArray() 之前没有完成?

我会尝试重构,但它尚未经过测试,在此之前我想了解为什么它在 QA 中无法正常工作。 示例我想如何重构它:

    const example = source.pipe(
      groupBy(x => x.id),
      mergeMap(group => group.pipe(toArray())),
      map(g => {
        return {
          id: g[0].secondProperty,
          secondProperty: g[0].thirdProperty,
          budgetDays: _.sumBy(g, 'budgetDays'), 
        }
      }),
      toArray()
    );

更新

我试过重构版本:

private groupAndSumUp(pricingLevels: SingleContractWorkPackagePricingLevel[]) {
  from(pricingLevels).pipe(
    groupBy(x => x.masterAgreementPricingLevelId),
    mergeMap(group => group.pipe(toArray())),
    map(g => {
      return {
        id: g[0].masterAgreementPricingLevelId,
        masterAgreementPricingLevel: g[0].budgetDays,
        budgetDays: _.sumBy(g, 'budgetDays'), 
      }
    }),
    toArray()
  ).subscribe(d => this.budgetDays = d); 
}

现在我有错误: ERROR TypeError: Vn.a.from(...).groupBy 不是函数

我的进口:

import { from } from 'rxjs';
import { groupBy, map, mergeMap, toArray } from 'rxjs/operators';
import * as _ from 'lodash';
import { Observable } from 'rxjs/Observable';

Rxjs 版本:

"rxjs": "^6.6.7",
"rxjs-compat": "^6.6.7",

我的错误发生了变化:

ERROR TypeError: Cannot set property 'checked' of undefined
    at 7-es2015.23db786fc6208d7e819e.js:1
    at Array.forEach (<anonymous>)
    at t.setSelectedDays (7-es2015.23db786fc6208d7e819e.js:1)
    at t.ngOnChanges (7-es2015.23db786fc6208d7e819e.js:1)
    at t.We (main-es2015.1967bf97e6c77b08a1d5.js:1)
    at Ut (main-es2015.1967bf97e6c77b08a1d5.js:1)
    at $t (main-es2015.1967bf97e6c77b08a1d5.js:1)
    at Vt (main-es2015.1967bf97e6c77b08a1d5.js:1)
    at Zi (main-es2015.1967bf97e6c77b08a1d5.js:1)
    at main-es2015.1967bf97e6c77b08a1d5.js:1

【问题讨论】:

  • 可能与如何打包应用程序以在 QA 中运行有关。您能否分享更多关于您如何在本地和 QA 上运行代码的信息?

标签: angular typescript rxjs rxjs-pipeable-operators


【解决方案1】:

我只能假设它不起作用,因为它找不到 .map 运算符。如果您不使用以下 import 语句,这会以旧的 rxjs 方式发生:

import 'rxjs/add/operator/map';

现在可能是在 QA 环境中,应用程序已经过优化和缩小,而 map 运算符再也找不到了。如果您尚未访问某个页面(其中包含该导入),则可能会发生这种情况。要“修复”它,您需要将该导入放入实际使用 map 运算符的文件中。

但是,您的重构显然是适当的修复。

【讨论】:

  • 我刚刚在我的 DEV 环境中进行了测试,我的意思是我的重构版本,现在我有一个错误:main-es2015.8595b64341733ca9606d.js:1 ERROR TypeError: Vn.a.from(. ..).groupBy 不是函数
  • @RobertDaraż 似乎您没有使用.pipe
  • 抱歉,我粘贴了错误的代码部分。现在是正确的
  • @RobertDaraż 你能告诉我你正在使用的导入,你有什么 rxjs 版本?
  • 我刚刚编辑了帖子。当前错误已更改,您可以在帖子的更新版本中看到
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多