无法使用NodeJ和Angular 更新记录

e37o9pze  于 2022-11-29  发布在  Node.js
关注(0)|答案(2)|浏览(109)

我是一个初学者的nodejs与Angular 。我创建一个简单的crud应用程序使用nodejs与Angular 。我可以添加和删除和查看记录以及。但我不能更新记录。当我在表单上进行更改,并点击提交按钮,我得到的错误是我通过控制台检查。

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

同时,节点js服务器停止,并在命令提示符下给予错误。

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'

}
到目前为止,我所做的尝试附在下面。请解决这个问题。

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

  }

}

}
节点js更新功能

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

          
       });

   });

}
gr8qqesn

gr8qqesn1#

评论中讨论后总结:在setUpdate(data: any)中设置了函数this.currentEmployeeID = data.id;。似乎data.id未定义,这导致未定义在URL路径中被序列化。
这会导致后端出错。

hjqgdpho

hjqgdpho2#

听着,我需要知道你在哪里调用setUpdate()方法但是你可以试着去掉属性名称中的双引号:

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

相关问题