angular + nestjs 中的数据库更新问题

database update problem in angular + nestjs

你好,我是 angular 和 nestjs 的初学者,我正在尝试使用前端更新数据库中的特定行,但它没有更新它,但是,我可以很好地插入新数据,我的文件看起来像这样,

前端

update.component.html

<div>
    <form class="form">
      <div class="pageTitle title"> Update Structure/Department Information </div>
      <div class="secondaryTitle title">Insert Department Name you want to update</div>
      <input  type="text" class="name formEntry" placeholder="Department Name" name="something" [(ngModel)] = "MyDept"/>
      <button class="get formEntry" (click)="getManager()">Get</button>
          <ul class="flight_headers" *ngFor="let org of barg">
            <li class="departs cell"> Description: <input type="text" name="DN" [(ngModel)]="org.description"/> </li><br/>
            <li class="arrives cell"> managing department:  <input type="text" name="bDN" [(ngModel)]="org.managing_department"/> </li> <br/>
             <button class="get formEntry" (click)="update(org)">Update</button>
         </ul>

update.component.ts

import { Component, OnInit } from '@angular/core';
import { OrganiService } from '../organi.service';
import { Organi } from '../organi.model';

@Component({
  selector: 'app-update',
  templateUrl: './update.component.html',
  styleUrls: ['./update.component.scss']
})
export class UpdateComponent implements OnInit {

  constructor(private organiService: OrganiService) { }


  barg: Organi[];

  ngOnInit(): void {
  }

  getManager(): void{
      let name:string = this.MyDept;
      this.organiService.getManagingStructures(name).subscribe(data=>{
        this.barg = data;
      })
      }

  update(org: Organi): void{
    this.organiService.updateStructure(org);
    
    window.location.reload()
  }
}

organi.service.ts

import { Injectable, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient} from '@angular/common/http';
import { Organi } from './organi.model';

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

  constructor(private http: HttpClient) {
  }
  getManagingStructures(name: string): Observable<any> {
    return this.http.get('http://localhost:3000/structures/query/'+name);
  }
  getSubordinateStructures(name: string): Observable<any> {
    return this.http.get('http://localhost:3000/structures/query2/'+name);
  }

  postStructure(org: Organi) {
    return this.http.post('http://localhost:3000/structures',org).subscribe(data =>{
      console.log("New Structure Created!")
    })
  }

  updateStructure(myData: Organi) {
    return this.http.post('http://localhost:3000/structures/update',myData);
  }

  deleteStructure(id: number) {
    
  }
}

后端

structures.controller.ts

import { Controller, Get, Post, Param, Body, Put, Delete, Patch } from '@nestjs/common';
import { StructuresService } from './structures.service';
import { Structure } from './structure.model';
import { Organization } from './structure.entity';

@Controller('structures')
export class StructuresController {
  constructor(private readonly structuresService: StructuresService) {}


  @Get()
  findAll() {
    return this.structuresService.findAll();
  }

  @Get("query/:name")
  async query(@Param('name') name): Promise<any> {
    return this.structuresService.query(name);
  }
  @Get("query2/:boss")
  async query2(@Param('boss') boss): Promise<any> {
    return this.structuresService.query2(boss);
  }
  
  @Post()
  async create(@Body() structure: Structure): Promise<Organization[]> {
    return this.structuresService.create(structure);
  }

  @Patch("update")
  async update(@Body() structure: Structure): Promise<any> {
    return this.structuresService.update(structure);
  }

 /* @Delete(':id')
  remove(@Param('id') id: string) {
    return this.structuresService.remove(+id);
  } */
}

structures.services.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, UpdateResult } from 'typeorm';
import { Organization } from './structure.entity';
import { Structure } from './structure.model';
//import { structure } from './structure.model'
@Injectable()
export class StructuresService {

    constructor(
    @InjectRepository(Organization)
    private readonly structureRepository: Repository<Organization>,
    ){}

  async findAll(): Promise<any> {
    return this.structureRepository.find();
  }
  
  async findManager(CEO: string): Promise<any> {
    return this.structureRepository.find();
  }

  async query(Myname: string): Promise<any> {
   return await this.structureRepository.find({name: Myname});
  }
  async query2(boss: string): Promise<any> {
   return await this.structureRepository.find({managing_department: boss});
  }

  async create(structure: Structure): Promise<any> {
    return await this.structureRepository.save(structure);
  }

  async update(structure: Structure): Promise<UpdateResult> {
    return await this.structureRepository.update(structure.id, structure);
  }

}

似乎您实际上并没有调用更新方法...在您的 OrganiSevice 中的 updateStructure() 中,http.post() 从未被触发,因为没有 subscribe() 和 toPromise()。

显然,CORS 功能阻止了请求通过,因为我使用的是 @Patch 装饰器,当我将其更改为 @Put 装饰器时它工作正常。

您在 UpdateComponent class

中忘记了 MyDept 属性

更新

您忘记订阅 updateStructure(org) 方法

如果您不订阅可观察方法,它不会调用