Angular Сервис не выполняет switchmap - PullRequest
0 голосов
/ 20 марта 2020

У меня есть карта переключателей, которая не будет go внутри. Я работал сегодня раньше, но по какой-то причине теперь он не выводит alert("INSIDE SWITCHMAP") или console.logs внутри карты переключателей.

Также этот маленький блок предупреждений

    alert(userId);
    alert(currentPage);
    alert(auctionId);

выводит правильные значения, поэтому ни одно из них не равно нулю.

Есть предложения? Я ценю помощь!

import { Injectable, OnDestroy } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Router } from "@angular/router";
import { Subject } from "rxjs";
import { Bidding } from "./bidding.model";
import { Watching } from "../watching/watching.model";
import { PasswordReset } from "../reset-password/password.model";
import { environment } from 'src/environments/environment';
import { takeUntil, switchMap } from 'rxjs/operators';

@Injectable({ providedIn: "root" })
export class BiddingService implements OnDestroy {
  destroy = new Subject();
  constructor(private http: HttpClient, private router: Router) { }

  submitBid(
    auctionId: string,
    currentBid: string,
    userId: string,
    biddingGroup: string[],
    auctionEndDateTime: string,
    lastBidderName: string,
    runnerUpBidderName: string,
    runnerUpBid: string,
    reservePriceBeat: boolean,
    currentPage: string
  ) {

    let bidderName;
    let listingHasBeenFound = false;

    const bidding: Bidding = {
      id: auctionId,
      bidderId: userId,
      lastBidTimeStamp: null,
      currentBid: currentBid,
      runnerUpBidderName: runnerUpBidderName,
      runnerUpBid: runnerUpBid,
      lastBidderName: lastBidderName,
      biddingGroup: biddingGroup,
      auctionEndDateTime: auctionEndDateTime,
      reservePriceBeat: reservePriceBeat
    };

    alert("OUTSIDE SWITCHMAP")
    alert(userId);
    alert(currentPage);
    alert(auctionId);

    return this.getUserNameandListingPage(userId, currentPage, auctionId).pipe(
      switchMap(res => {
        alert("INSIDE SWITCHMAP")
        bidderName = res.posts.username;
        listingHasBeenFound = res.listingHasBeenFound;
        console.log("bidderName");
        console.log(bidderName);
        console.log(listingHasBeenFound);

        console.log(res);

        return this.http.patch(
          environment.api_url + `/listings/${bidding.id}`,
          bidding
        );
      })
    );

  }



  getUserNameandListingPage(id: string, currentPage: string, listingId: string) {


    const params = new HttpParams()
      .set("userIdBidder", id)
      .set("listingId", listingId)
      .set("currentPage", currentPage);

    return this.http.get<{ posts: any; listingHasBeenFound: boolean }>(
      environment.api_url + `/user/${id}`, { params }
    );
  }



  ngOnDestroy() {
    this.destroy.next();
    this.destroy.complete();
  }

}

компонент verifyBid

import {
  Component,
  Output,
  EventEmitter,
  Inject,
  OnInit,
  OnDestroy
} from "@angular/core";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material";
import { AuctionListingsComponent } from "../auction-listings/auction-listings.component";
import { BiddingService } from "../auction-listings/bidding.service";
import { ToastrService } from "ngx-toastr";
import * as Ably from "ably";
import { SubmitListingService } from "../submit-listing/submit-auction.service";
import { Subject } from "rxjs";
import { startWith, filter, takeUntil } from "rxjs/operators";
let api = process.env.ablyAPi;
let options: Ably.Types.ClientOptions = { key: api };
let client = new Ably.Realtime(options); /* inferred type Ably.Realtime */
let channel = client.channels.get(
  "auctions"
); /* inferred type Ably.Types.RealtimeChannel */

@Component({
  selector: "app-confirmation-dialog",
  templateUrl: "./confirmation-dialog.component.html",
  styleUrls: ["./confirmation-dialog.component.css"]
})
export class ConfirmationDialogComponent implements OnInit, OnDestroy {
  currentBid: string;
  bidInput: string;
  auctionId: string;
  userId: string;
  runnerUpBidderName: string;
  runnerUpBid: string;
  bidderName: string;
  biddingGroup: string[];
  currentData: any[];
  auctionEndDateTime: string;
  currentPage: string;
  success: boolean;
  listingHasBeenFound: boolean;
  username: string;
  reservePriceBeat = false;
  destroy = new Subject();

  constructor(
    public dialogRef: MatDialogRef<AuctionListingsComponent>,
    @Inject(MAT_DIALOG_DATA) public data: ConfirmationDialogComponent,
    private biddingService: BiddingService,
    private toastr: ToastrService,
    private submitListingService: SubmitListingService
  ) { }

  ngOnInit() {
    this.auctionId = this.data.auctionId;
    this.runnerUpBid = this.data.currentBid;
    this.bidInput = this.data.bidInput;
    this.userId = this.data.userId;
    this.runnerUpBidderName = this.data.runnerUpBidderName;
    this.biddingGroup = this.data.biddingGroup;
    this.currentPage = this.data.currentPage;
    this.reservePriceBeat = this.data.reservePriceBeat;
    this.auctionEndDateTime = this.data.auctionEndDateTime;
    this.listingHasBeenFound = this.data.listingHasBeenFound;
    this.bidderName = this.data.bidderName;

  }

  ngOnDestroy() {
    this.destroy.next();
    this.destroy.complete();
  }

  confirmBid() {
    const data = {
      auctionId: this.auctionId,
      lastBid: this.bidInput,
      lastBidderId: this.userId,
      username: this.username
    };
    // Optionally, you can use a callback to be notified of success or failure
    channel.publish("auctions", data);
    this.biddingService
      .submitBid(
        this.auctionId,
        this.bidInput,
        this.userId,
        this.biddingGroup,
        this.auctionEndDateTime,
        this.username,
        this.runnerUpBidderName,
        this.runnerUpBid,
        this.reservePriceBeat,
        this.currentPage
      )
      .pipe(takeUntil(this.destroy))
      .subscribe(res => {
        console.log("RES");
        console.log(res);
      });

    this.toastr.success("Your bid has been submitted", "", {
      timeOut: 2000
    });

    this.success = true;

    var returnData = [
      {
        newEndTime: this.auctionEndDateTime,
        auctionId: this.auctionId,
        success: this.success,
        username: this.username
      }
    ];

    this.dialogRef.close(returnData);
  }

  cancel() {

    this.dialogRef.close(true);
    this.success = false;
  }
}

вызов API

if (req.query.userIdBidder) {
    console.log("correct path!");
    console.log(req.query.userIdBidder);

    Post.find({
      $and: [
        { auctionType: { $eq: "publicAuction" } },
        { auctionEndDateTime: { $gte: Date.now() } },
        { blacklistGroup: { $ne: req.query.bidderUserId } },
        // { startTime: { $lte: Date.now() } }
      ]
    }) //test
      .skip(10 * (req.query.currentPage - 1))
      .sort({ dateCreated: -1 })
      .limit(10)
      .then(listings => {

        console.log("listings");

        let listingHasBeenFound = false;
        for (let i = 0; i < listings.length; i++) {

          console.log("i");
          console.log(i);
          console.log(req.query.listingId);
          console.log(listings[i]._id);
          if (listings[i]._id.toString().trim() === req.query.listingId.toString().trim()) {
            listingHasBeenFound = true;
            break;
          }

        }

        console.log("CURRENT PAGE " + req.query.listingId);


        User.findById({ _id: req.query.userIdBidder })
          .select("username")
          .then(documents => {
            console.log("HERE WE ARE")
            res.status(200).json({
              message: "User account located!",
              posts: documents,
              listingHasBeenFound: listingHasBeenFound
            });
          });
      }).catch((res) => {

        console.log("ERROR");
        console.log(res);
      });

  }

1 Ответ

1 голос
/ 20 марта 2020

Немного измените ваш метод confirmBid() в вашем ConfirmationDialogComponent (переместите весь код после закрывающей скобки subscribe внутрь функции подписчика):

confirmBid() {
    const data = {
      auctionId: this.auctionId,
      lastBid: this.bidInput,
      lastBidderId: this.userId,
      username: this.username
    };
    // Optionally, you can use a callback to be notified of success or failure
    channel.publish("auctions", data);
    this.biddingService
      .submitBid(
        this.auctionId,
        this.bidInput,
        this.userId,
        this.biddingGroup,
        this.auctionEndDateTime,
        this.username,
        this.runnerUpBidderName,
        this.runnerUpBid,
        this.reservePriceBeat,
        this.currentPage
      )
      .pipe(takeUntil(this.destroy))
      .subscribe(res => {
          this.toastr.success("Your bid has been submitted", "", {
            timeOut: 2000
          });

          this.success = true;

          const returnData = [{
              newEndTime: this.auctionEndDateTime,
              auctionId: this.auctionId,
              success: this.success,
              username: this.username
          }];

          this.dialogRef.close(returnData);
      });
  }
...