Angular Service没有将值传递给后端-报头错误问题

时间:2019-11-09 20:09:25

标签: angular express mongoose

我不知道为什么我的值没有从角度服务传递到express / mongoose PATCH调用。它进入表中的文档,但是将currentBid更新为null,并且lastBidTimeStamp和bidderId不会更新。

如果我这样使用邮递员:

https://postimg.cc/1nQnX3zK

两个值也不以这种方式更新。邮递员中只有currentBid更新。

我在服务中做了console.log(lastBidTimeStamp),它正确地输出了日期,因此它在服务中接收了值。

不确定我在做什么错。还在学习。任何帮助表示赞赏。

bidding.service.ts

cc.pl.300.bin

app.js

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Router } from '@angular/router';
import { Subject } from 'rxjs';
import { Bidding } from './bidding.model';


@Injectable({ providedIn: 'root' })
export class BiddingService {


  constructor(private http: HttpClient, private router: Router) { }


  submitBid(auctionId: string, currentBid: string, lastBidTimeStamp: Date, userId: string) {



    const bidding: Bidding = {
      id: auctionId,
      bidderId: userId,
      lastBidTimeStamp: lastBidTimeStamp,
      bidValue: currentBid
    };

    return this.http.patch(`http://localhost:3000/api/listings/${bidding.id}`, bidding,
    );

  }

}

2 个答案:

答案 0 :(得分:0)

似乎不需要在您的方法内部调用Post.update()方法。 此外,您的数据来自正面:

enter image description here

As docs of express.js says:

  

使用指定的路径将HTTP PUT请求路由到指定的路径   回调函数。

因此您的代码应如下所示:

app.put('/api/listings/:id', (req, res) {        
   // Retrieve the data to insert from the PUT body
   var data = [
       req.body.id,
       req.body.bidderId,
       req.body.bidValue
   ];

   // Then you can do what you want with your data

});

More info about how to build REST API is can be read here.

答案 1 :(得分:0)

我可以处理以下放置请求

app.put('/api/listings/:id', (req, res) => {

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