0

Hello I am beginner both in angular and nestjs and I am trying to update a specific row in my database using the front end and it is not updating it but, I can insert new data just fine, my files look like this,

Front-End

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

Back-end

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

}

3 Answers 3

1

Seems like you are not actually calling the update method... inside updateStructure() in your OrganiSevice the http.post() is never triggered as there is no subscribe() nor toPromise().

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Apparently, the CORS feature was blocking the request from going through because I was using the @Patch decorator, when I changed it to @Put decorator it worked fine.

Comments

0

you forgot MyDept property in UpdateComponent class

UPDATE

you forgot subscribe updateStructure(org) method

if you don't subscribe observable method it won't call

3 Comments

Yeah, I must have left it out while posting the question
@AdugnaTadesse subscribe updateStructure(org) method
Yes, I have found out the hard way, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.