简体   繁体   English

Angular订阅和observable重复获取数据

[英]Angular subscription and observable repeatedly get data

I am having problem using subscription and observable我在使用订阅和可观察时遇到问题

here's my code这是我的代码

this is my inventory.service.ts这是我的 inventory.service.ts

getInventoryList = (page: string,pageSize,size) => {
    const userLocation = this.authService.getUserLocation();
    let queryParams = `?page=${page}&loc=${userLocation}&size=${size}&pageSize=${pageSize}`;
      this.http
      .get<{ success: boolean, message: string, inventoryList: any }>(BACKEND_URL_item + '/getInventoryList' + queryParams)
      .pipe(retry(3), catchError((data) => {
        return EMPTY;
      }),map((data) => {
        if (page === 'inventory') {
          return {
            extractedInventoryList: data.inventoryList.map((item: any) => {
              return {
                itemId: item._id,
                itemID: item.itemID,
                itemName: item.itemName,
                itemSellingPrice: item.itemSellingPrice,
                itemPurchasePrice: item.itemPurchasePrice,
                itemAveragePurchasePrice: item.itemAveragePurchasePrice,
                itemBaseUnit: item.itemBaseUnit,
                itemCategory: item.itemCategory,
                itemReorderPoint: item.itemReorderPoint,
                itemTotalQuantity: item.itemTotalQuantity,
                itemSumQuantity: item.itemSumQuantity,
                itemLocation: item.itemLocation,
                itemSubLocation: item.itemSubLocation
              };
            }),
            success: data.success,
            message: data.message
          };
        } else {
          return {
            extractedInventoryList: data.inventoryList.map((item: any) => {
              return {
                itemId: item._id,
                itemName: item.itemName,
                itemTotalQuantity: item.itemTotalQuantity,
                itemLocation: item.itemLocation,
                itemSubLocation: item.itemSubLocation
              };
            }),
            success: data.success,
            message: data.message
          };
        }
      }))
      .subscribe((transformedData) => {
          this.inventoryList = transformedData.extractedInventoryList;
          this.inventoryListObserver.next({
            inventoryList: [...this.inventoryList],
            success: transformedData.success,
            message: transformedData.message
          });

        }
      });

  }

  getInventoryListListener = () => {
    return this.inventoryListObserver.asObservable();
  }

here's my inventory.component.ts这是我的 inventory.component.ts

getInventoryItem(pageSize,size) {

    for(let x=0;x<size;x= x+10){

          this.inventoryService.getInventoryList('inventory',pageSize,x);
          this.itemListSubscription = this.inventoryService
        .getInventoryListListener()
        .subscribe((responseData: { inventoryList: IItem[], success: boolean, message: string }) => {
          if (!responseData.success) {
            this.spinner.stop();

          } else {
            this.itemList = this.itemList.concat(responseData.inventoryList);
            this.spinner.stop();
            this.itemListBackup = this.itemList;
          }
           this.showToasts(responseData.message, responseData.success);
        });




    }

  }

I'm trying to get data every 10 items.. I am using mongodb and it returns exactly what I want but when I get it through Angular it inserts some duplicate arrays to the itemList我正在尝试每 10 个项目获取数据.. 我正在使用 mongodb 它返回正是我想要的但是当我通过 Angular 得到它时它会插入一些重复的 arrays

can anyone explain why is that?谁能解释这是为什么?

First would return the http Observable首先将返回 http Observable

public getInventoryListo(page: string, pageSize: number, size: number): Observable<any> {
  const userLocation = this.authService.getUserLcation();
  const queryParams = `?page=${page}&loc=${userLocation}&size=${size}&pageSize=${pageSize}`;
  return this.http
    .get<{ success: boolean, message: string, inventoryList: any }>(BACKEND_URL_item + '/getInventoryList' + queryParams)
    .pipe(
      .map((data) => {
        if (page.toLowerCase() === 'inventory') {
          return {
            extractedInventoryList: data.inventoryList.map((item: any) => {
              return {
                itemId: item._id,
                itemID: item.itemID,
                itemName: item.itemName,
                itemSellingPrice: item.itemSellingPrice,
                itemPurchasePrice: item.itemPurchasePrice,
                itemAveragePurchasePrice: item.itemAveragePurchasePrice,
                itemBaseUnit: item.itemBaseUnit,
                itemCategory: item.itemCategory,
                itemReorderPoint: item.itemReorderPoint,
                itemTotalQuantity: item.itemTotalQuantity,
                itemSumQuantity: item.itemSumQuantity,
                itemLocation: item.itemLocation,
                itemSubLocation: item.itemSubLocation
              };
            }),
            success: data.success,
            message: data.message
          };
        } else {
          return {
            extractedInventoryList: data.inventoryList.map((item: any) => {
              return {
                itemId: item._id,
                itemName: item.itemName,
                itemTotalQuantity: item.itemTotalQuantity,
                itemLocation: item.itemLocation,
                itemSubLocation: item.itemSubLocation
              };
            }),
            success: data.success,
            message: data.message
          };
        }
      })
      retry(3), 
      catchError((data) => {
        return EMPTY;
      })
    );
}

And then just make 1 request insted to x-times calling the api然后只需将 1 个请求安装到 x 次调用 api

getInventoryItem(pageSize: number, maxSize: number): void {
  this.itemList = [];

  for (let i = 0; i < maxSize;  i += 10) {
    this.inventoryService.getInventoryList('inventory', pageSize, x).subscribe((response: any) => {
      this.spinner.stop();
            this.showToasts(responseData.message, responseData.success);

      if (response.success) {
        for (let j of response.inventoryList) this.itemList.push(j);
        this.itemListBackup = this.itemList;
      }
    });
  }
}

Also if you use ngFor to display the list in your template you can use the slice pipe .此外,如果您使用ngFor在模板中显示列表,则可以使用slice pipe

eg: *ngFor="let item of itemList | slice: 0: 10" This will show the first 10 the entries.例如: *ngFor="let item of itemList | slice: 0: 10"这将显示前 10 个条目。

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

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