user6680
user6680

Reputation: 139

Angular HTTP PUT not reaching expressjs

I have a PUT request that I'm trying to have hit the backend, but for some reason, it never reaches it. What's odd is the if(req.body.bidderId){} hits no problem, but not the if(req.body.watchingGroup){}

The watching angular service uses identical code to the bidderId so I don't know what's different between the two where only one would reach the endpoint? Whats wrong with the addToWatchList call? I did testing and both console.log statements in code block return the correct value. So the data is ready to be passes, but is never received.

console.log("MADE IT TO LISTINGS BACKEND"); never outputs for watchingGroup scenario

watching.service.ts

addToWatchList(id: string, watchingGroup: string[]) {
    const watching: Watching = {
      id: id,
      watchingGroup: watchingGroup
    };
    console.log("Here are the values being passed to backend");
    console.log(watching.id);
    console.log(watching.watchingGroup);
    console.log(watching);
    return this.http.put(`http://localhost:3000/api/listings/${watching.id}`, watching,
    );
  }

app.js

app.put("/api/listings/:id", (req, res) => {
  console.log("MADE IT TO LISTINGS BACKEND");

  if (req.body.biddingGroup) {
    console.log("bidding has been received");

    Post.findByIdAndUpdate(
      { _id: req.params.id },
      {
        currentBid: req.body.currentBid,
        lastBidTimeStamp: Date.now(),
        bidderId: req.body.bidderId,
        auctionEndDateTime: req.body.auctionEndDateTime,
        biddingGroup: req.body.biddingGroup,
        lastBidTimeStamp: req.body.lastBidTimeStamp
      },
      function(err, docs) {
        if (err) res.json(err);
        else {
          console.log(docs);
        }
      }
    );
  }
  if (req.body.watchingGroup) {
    console.log("watching has been received");

    Post.findByIdAndUpdate(
      { _id: req.params.id },
      {
        watchingGroup: req.body.watchingGroup
      },
      function(err, docs) {
        if (err) res.json(err);
        else {
          console.log(docs);
        }
      }
    );
  }
});

addToWatchList

 addToWatchList(
    auctionId: string,
    watchingGroup: string[]
  ) {
    this.watchItStatus = true;
    this.userId = localStorage.getItem("userId: ");

    var unique = watchingGroup.filter(function(elem, index, self) {
      return index === self.indexOf(elem);
    });
    this.uniqueResult = unique;
    watchingGroup.push(this.userId);
    this.watchListService.addToWatchList(auctionId, this.uniqueResult);
  }

Upvotes: 0

Views: 56

Answers (1)

hbamithkumara
hbamithkumara

Reputation: 2544

As i suspected you're not subscribing to it. It's weird but you need to subscribe to it.

this.watchListService.addToWatchList(auctionId, this.uniqueResult).subscribe(
  (res) => {
    // Handle success response
    console.log("SUCCESS");
  },
  (err) => {
    // Handle error response
    console.log("ERROR");
  }
);

Upvotes: 1

Related Questions