首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular + nestjs中的数据库更新问题

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

Stack Overflow用户
提问于 2021-09-19 09:30:31
回答 3查看 63关注 0票数 0

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

前端

update.component.html

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
  }

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-09-19 11:46:36

显然,CORS功能阻止了请求通过,因为我正在使用@Patch装饰器,当我将它改为@Put装饰器时,它工作得很好。

票数 0
EN

Stack Overflow用户

发布于 2021-09-19 11:08:34

看起来您实际上并没有调用update方法...在OrganiSevice的updateStructure()中,http.post()永远不会被触发,因为没有subscribe()和toPromise()。

票数 1
EN

Stack Overflow用户

发布于 2021-09-19 12:10:02

您忘记了UpdateComponent类中的MyDept属性

更新

您忘记了subscribe updateStructure(组织)方法

如果不订阅observable方法,它将不会调用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69242002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档