简体   繁体   English

Put方法不使用后端的NodeJS更新Angular7中的MongoDB

[英]Put method not updating MongoDB in Angular7 with NodeJS on the backend

I have this Angular7 app with NodeJS and MongoDB on the backend. 我在后端有这个带有NodeJS和MongoDB的Angular7应用程序。 I have tested out my put method with Postman and it works perfectly. 我已经用Postman测试了我的put方法,并且效果很好。 The error lies in my Angular service component or possibly the component module that is using the service. 错误出在我的Angular服务组件中,或者可能在使用该服务的组件模块中。 No error messages are being displayed in the console. 控制台中未显示任何错误消息。 I have isolated it down to this--all the data is making it to the service, so the breakdown is between the service component and the Node api. 我已经将其隔离了下来-所有数据都将其存储到服务中,因此故障发生在服务组件和Node api之间。 Here's the method in my service.ts file: 这是我的service.ts文件中的方法:

updateSavings(pin: string, data){
const url = `api/accounts/${pin}`;
console.log(data);
return this._http.put(url, data, httpOptions)
  .pipe(catchError(this.handleError)
  );
}

Here is my Api.JS method that works fine with Postman: 这是我的Api.JS方法,适用于Postman:

router.put('/accounts/:pin', function(req, res, next) {
  Accounts.findOneAndUpdate({pin: req.params.pin}, {savings: 
  req.body.savings}).then(function() {
  //console.log(req.body.savings);
  Accounts.findOne({pin:req.params.pin}).then(function(account){
  //console.log(res.send(account))
    }) 
  })
})

Here is my component method that uses the services method: 这是我使用services方法的组件方法:

depositSavings(data){
  let y = this.accountsService.updateSavings(this.appComponent.rightPin, 
  data);
  console.log(this.appComponent.rightPin);
  return y;
  }
} 

Here is my template to show you what is going on there: 这是我的模板,向您展示正在发生的事情:

<input 
  type="text"
  id="input"
  [(ngModel)]="data"
  [ngModelOptions]="{standalone: true}">
  <br><br>
  <button (click)="depositSavings(data)">Submit</button> 

Any ideas what I have wrong? 有什么主意我错了吗? Thanks in advance guys. 在此先感谢大家。

You have to pass current _id as a first parameter and then whole object for update 您必须传递当前的_id作为第一个参数,然后传递整个对象进行更新

router.put('/accounts/:pin', function(req, res, next) {
  Accounts.findOneAndUpdate(req.params._id, {pin: req.params.pin, savings: 
  req.body.savings}).then(function() {
  //console.log(req.body.savings);
  Accounts.findOne({pin:req.params.pin}).then(function(account){
  //console.log(res.send(account))
    }) 
  })
})

I handled the problem. 我解决了这个问题。 As there were no error messages with this, it made it more challenging to figure this one out. 由于没有与此相关的错误消息,因此弄清楚这一点变得更具挑战性。 What I was missing was this inside of my service method: 我所缺少的是我的服务方法内部的内容:

return this._http.put(url, {"savings": data}, httpOptions).subscribe(data => 
{console.log("PUT request successful ", data)})

In place of just data , you use {"savings": data} . 仅仅是在原地的data ,您可以使用{"savings": data}

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

相关问题 如何使用Node.js脚本为复杂的json对象定义PUT方法以更新mongoDB中的记录? - How to define PUT method for complex json objects for updating records in mongoDB using nodejs scripts? Angular Put 方法不更新数据 - Angular Put Method not updating data 表单字段未使用mongodb和nodejs更新到数据库Angular - Form field not updating to database Angular with mongodb and nodejs 如何在 angular 中获取登录用户的 ID(MONGODB,后端的 NODEJS) - How to get logged user's Id in angular (MONGODB, NODEJS for backend) 无法连接到后端到 nodejs 和 MongoDB - Unable to connect to Backend to nodejs and MongoDB 从 mongodb 检索图像并使用 angular7 显示它 - retrieve an image from mongodb and display it using angular7 通过HTTPS在NodeJS后端上进行Angular - Angular on NodeJS backend via HTTPS 有条件地在后端更新/删除文档。我是不是该?我是否分配了PUT,POST或DELETE http方法? - Conditionally updating / deleting a document in backend. Should I? Do I assign a PUT, POST, or DELETE http method? 如何以角度设置图表的数据,该数据取自存储在 mongodb 中并显示在 html 页面上的后端 nodejs api - How to set the data of chart in angular which is taken from the backend nodejs api that is store in mongodb and showing on the html page Angular 2 - MEAN MongoDB NodeJS - Angular 2 - MEAN MongoDB NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM