【发布时间】:2019-07-18 11:12:58
【问题描述】:
我有这个 Angular7 应用程序,后端带有 NodeJS 和 MongoDB。我已经用 Postman 测试了我的 put 方法,它运行良好。错误在于我的 Angular 服务组件或可能是使用该服务的组件模块。控制台中未显示任何错误消息。我已将其隔离到这一点——所有数据都在进入服务,因此故障发生在服务组件和 Node api 之间。这是我的 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)
);
}
这是我的 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))
})
})
})
这是我使用 services 方法的组件方法:
depositSavings(data){
let y = this.accountsService.updateSavings(this.appComponent.rightPin,
data);
console.log(this.appComponent.rightPin);
return y;
}
}
这是我的模板,向您展示那里发生了什么:
<input
type="text"
id="input"
[(ngModel)]="data"
[ngModelOptions]="{standalone: true}">
<br><br>
<button (click)="depositSavings(data)">Submit</button>
有什么想法我错了吗?提前谢谢各位。
【问题讨论】:
标签: node.js angular mongodb mongoose angular7