使用angular 7服务和节点JS REST API在mongoDB中未更新数据

时间:2019-01-18 09:21:45

标签: node.js angular angular6

在editCategory()函数中,我将ID存储到本地存储中

category-component.ts

editCategory(category: CategoryClass) {
if (confirm('Are you sure you want to edit this category?')) {
  localStorage.removeItem('editCategoryId');
  localStorage.setItem('editCategoryId', category._id.toString());
  this.router.navigate(['/updateCategory']);} else{this.flashAlert.show(' Deny to edit!', { cssClass: 'alert-primary', timeout: 2000 });}}

category-service.ts

updateCategory(category: CategoryClass) {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.put('http://localhost:3000/api/updateCategory/' + 
category._id, category, { headers: headers })
.pipe(map(res => res.json()));}

edit-category.component.ts

在编辑类别组件的方法中。我正在从本地存储中获取ID,并借助此ID和uniqueCategory方法

获取唯一记录。
ngOnInit() {
const categoryId = localStorage.getItem('editCategoryId');
if (!categoryId) {
  alert('Invalid action');
  this.router.navigate(['category']);
  return;
} else {
  this.catService.uniqueCategory(categoryId)
  .subscribe( data => {
    this.category = data;
});}}



categoryUpdation() {
if (!this.categoryupdateForm.valid) {
  this.flashAlert.show('Please fill all the required field', { cssClass: 'alert-danger', timeout: 4000 });
  return false;
} else {
  this.catService.updateCategory(this.categoryupdateForm.value)
  .pipe(first())
  .subscribe(data => {
    this.router.navigate(['/category']);
  });
}}




category-service.ts

updateCategory(category: CategoryClass) {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.put('http://localhost:3000/api/updateCategory/' + 
category._id, category, { headers: headers })
.pipe(map(res => res.json()));}

0 个答案:

没有答案