user6680
user6680

Reputation: 139

switchMap - Subscription' is not assignable to parameter of type

I can't figure out why I'm getting the error

  Type 'Subscription' is not assignable to type 'ObservableInput<any>'.
    Property '[Symbol.iterator]' is missing in type 'Subscription' but required in type 'Iterable<any>'.ts(2345)

The error is happening on this line switchMap(res => {

I'm trying to chain two calls together. The first call gets the user's name and then the second call will add the name in the POST, but before I can POST, I need to verify results in switchmap, but I'm getting the error. Why is that?

angular service

  addListing(newListing: CreateListing) {
    let lastBidderId = "None";
    let bidderName = "None";
    let watchingGroup: string[] = [];
    const listing: Listing = {
      id: null,
      glassTitle: newListing.glassTitle,
      shippingCost: newListing.shippingCost,
      increments: newListing.increments,
      startingBid: newListing.startingBid,
      snipingRules: newListing.snipingRules,
      auctionType: newListing.auctionType,
      buyItNow: newListing.buyItNow,
      creator: null,
      auctionEndDateTime: newListing.auctionEndDateTime,
      currentBid: newListing.startingBid,
      lastBidTimeStamp: null,
      trackingNumber: null,
      mailCarrier: null,
      bidderId: lastBidderId,
      lastBidderName: bidderName,
      watchingGroup: null
    };

    return this.getArtistName(listing.creator).pipe(
      switchMap(res => {
        console.log("res");
        console.log(res);

        return this.http
          .post<{ message: string; listingId: string; creator: string }>(
            `http://localhost:3000/api/listings`,
            listing
          )
          .pipe(takeUntil(this.destroy))
          .subscribe(responseData => {
            const id = responseData.listingId;
            listing.id = id;
          });
      })
    );
  }

Upvotes: 0

Views: 947

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

The function passed to switchMap() is supposed to return an Observable. Yours returns a Subscription, not an Observable.

Don't subscribe. If you want to apply a side-effect (setting the listing id) when the observable emits, use the tap() operator.

But given that listing is a local variable, I fail to see why you're setting its ID in the first place. Just remove the .subscribe(...).

Upvotes: 1

Related Questions