Home > Mobile >  Couldn't Update the Records using NodeJs and Angular
Couldn't Update the Records using NodeJs and Angular

Time:11-28

I am a beginner of nodejs with angular.i creating a simple crud application using nodejs with angular.i can add and delete and view records well.but i couldn't update the records. when i make the changes on the form and click submit button i got the error was i checked through the console.

Failed to load resource: net::ERR_CONNECTION_REFUSED :9002/user/update/undefined:1 

at the same time node js server stop and give error on the command prompt .

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "false".] {
  code: 'ERR_UNHANDLED_REJECTION'

}

what i tried so far i attached below.please solve the problem.

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';

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

  EmployeeArray : any[] = [];
  isResultLoaded = false;
  isUpdateFormActive = false;

  first_name: string ="";
  last_name: string ="";
  email: string ="";
  password: string ="";

  currentEmployeeID = "";




  setUpdate(data: any) 
  {
   this.first_name = data.first_name;
   this.last_name = data.last_name;
   this.email = data.email;
   this.password = data.password;


   this.currentEmployeeID = data.id;
  }

  UpdateRecords()
  {
    let bodyData = {
     

      "first_name" : this.first_name,
      "last_name" : this.last_name,
      "email" : this.email,
      "password" : this.password,
    };
    
    this.http.patch("http://localhost:9002/user/update"  "/" this.currentEmployeeID,bodyData).subscribe((resultData: any)=>
    {
        console.log(resultData);
        alert("Employee Registered Updateddd")
       // this.getAllEmployee();
      
    });
  }



  save()
  {
    if(this.currentEmployeeID == '')
    {
        this.register();
    }
      else
      {
       this.UpdateRecords();
      }       

  }


  setDelete(data: any)
  {
    
    
    this.http.delete("http://localhost:9002/user/remove"  "/"  data.id).subscribe((resultData: any)=>
    {
        console.log(resultData);
        alert("Employee Deletedddd")
        this.getAllEmployee();
   
    });

  }


}

}

Node js Update Function

module.exports.updateOneUserDBService = (id,userDetais) => {
     
   console.log(userDetais);

   return new Promise(function myFn(resolve, reject) {

       userModel.findByIdAndUpdate(id,userDetais, function returnData(error, result) {

         if(error)
         {
               reject(false);
         }
         else
         {
            resolve(result);
         }

          
       });

   });

}

CodePudding user response:

Summary after discussion in comments: In setUpdate(data: any) function this.currentEmployeeID = data.id; was set. it seems like data.id was undefined, which lead to undefined being serialized in the URL path.

This lead to the error on backend side.

CodePudding user response:

Look, I need to see Where you call setUpdate() Method But you can try to remove double quotes from properties names :

let bodyData = {
      first_name: this.first_name,
      last_name: this.last_name,
      email: this.email,
      password: this.password,
    };
  • Related