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);
}
}

似乎你实际上并没有调用更新方法…在你的组织设备的updateStructure()中,http.post()永远不会被触发,因为没有subscribe()和toPromise()。

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

你忘了在UpdateComponent类中添加MyDept属性

更新你忘了订阅updateStructure(org)方法

如果你不订阅可观察对象方法它就不会调用

相关内容

  • 没有找到相关文章

最新更新