簡體   English   中英

Angular:我收到“未定義的 500 內部服務器錯誤”

[英]Angular: I'm getting "undefined 500 Internal Server Error"

先感謝您。 有人可以幫助我了解如何解決此錯誤嗎? 如果需要,我可以添加更多代碼細節,但是控制台指向我的ServiceController :並且終端指向我的模型架構:

再次感謝您。

控制台錯誤

控制台錯誤

import { Component, OnInit} from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';

// For Validation and Registration
import { BusinessCardServ, EventServ, JobServ } from '../models/services';

// For Backend API
import { BusinessCardService } from "../services/businesscards.service";
import { EventService } from "../services/events.service";
import { JobService } from "../services/jobs.service";
import { Subscription } from 'rxjs';


//const BusinessCard = require('models/BusinessCard');
@Component({
  selector: 'administer',
  templateUrl: './administer.component.html',
  styleUrls: ['./administer.component.scss']
})


export class AdministerComponent implements OnInit {

  listBusinesscards: BusinessCardServ [] = [];

  _subscription!: Subscription;

  businessCardForm: FormGroup;
  _id: any | null;
  
  constructor(
    // this for the business card form
    private modalService: NgbModal, 

    private bcfb: FormBuilder, 

    private _businessCardService: BusinessCardService, 

    private router: Router,

  ) {
    //Form Validation
  this.businessCardForm = this.bcfb.group({
      name: ['', Validators.required], 
      title: ['', Validators.required],
      email: ['', Validators.required],
      phone: ['', Validators.required],
      website: ['', ''],
      bName: ['', Validators.required],
      bServ: ['', Validators.required]
  });


  // Looks for the Html id="cancel"
  // let ref =  document.getElementById('close');
  // ref?.click();

}


openServPopUp(ServPopUp: any) {
  const modalRef = this.modalService.open(ServPopUp);
  modalRef.componentInstance;
}


public isServsCollapsed = true;



// To add... CRUD
createBusinessCard() {
  const BUSINESSCARD: BusinessCardServ = {
    name: this.businessCardForm.get('name')?.value,
    title: this.businessCardForm.get('title')?.value,
    email: this.businessCardForm.get('email')?.value,
    phone: this.businessCardForm.get('phone')?.value,
    website: this.businessCardForm.get('website')?.value,
    bName: this.businessCardForm.get('bName')?.value,
    bServ: this.businessCardForm.get('bServ')?.value,
  } 
  if(this._id !== null){
  console.log("If: Updating Business Card");
  this._businessCardService.updateBusinessCard(this._id, BUSINESSCARD).subscribe((data: any) ={
    this.router.navigate(['/administer']);
  }, error => {
    console.log(error);
    this.businessCardForm.reset();
  })
} else {
  console.log("Else: Creating Business Card");
  this._businessCardService.createBusinessCard(BUSINESSCARD).subscribe((data: any) => {
    this.router.navigate(['/administer']);
  }, error => {
    console.log(error);
    this.businessCardForm.reset();
  })
}
}



  ngOnInit(): void {

    // Business Card
    this.getBusinessCards();
  
    this._subscription = this._businessCardService.refresh$.subscribe(() => {
      this.getBusinessCards();
    });

    }
 

  
  ngOnDestroy(): void{
    this._subscription.unsubscribe();
    console.log("Observable Closed.");
  }


  getBusinessCards() {
    this._businessCardService.getBusinessCards().subscribe((data: any) => {
      console.log(data);
      // getting the data from the database
      this.listBusinesscards = data;
    }, (error: any) => {
      console.log(error);
    })
  }



  // To Automatically Fill the business card form with the data
  onEditBusinesscard(businesscard: any) {
    this._businessCardService.getBusinessCard(businesscard._id).subscribe((data: any) => {
    this.businessCardForm.controls['name'].setValue(businesscard.name);
    this.businessCardForm.controls['title'].setValue(businesscard.title);
    this.businessCardForm.controls['email'].setValue(businesscard.email);
    this.businessCardForm.controls['phone'].setValue(businesscard.phone);
    this.businessCardForm.controls['website'].setValue(businesscard.website);
    this.businessCardForm.controls['bName'].setValue(businesscard.bName);
    this.businessCardForm.controls['bServ'].setValue(businesscard.bServ);
    })
  }





  

我的服務控制器:

// Update request ------------------------------------
exports.updateBusinessCard = async(req, res) => {
    try {
        const { name, title, email, phone, website, bName, bServ } = req.body;
        let businessCard = await BusinessCard.findById(req.params._id);

        if (!businessCard) {
            res.status(404).json({ msg: 'Business card not found' });
        }

        businessCard.name = name;
        businessCard.title = title;
        businessCard.email = email;
        businessCard.phone = phone;
        businessCard.website = website;
        businessCard.bName = bName;
        businessCard.bServ = bServ;
        // findByOneAndUpdate
        // findByIdAndUpdate
        businessCard = await BusinessCard.findByOneAndUpdate({ _id: req.params._id }, businessCard, { new: true })
        res.json(businessCard);

    } catch (error) {
        console.log(error);
        res.status(500).send('Server error: Update business card!');
    }
}

我的模型架構:

const mongoose = require('mongoose');

const BusinessCardSchema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    title: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    },
    website: {
        type: String,
        required: false
    },
    bName: {
        type: String,
        required: true
    },
    bServ: {
        type: String,
        required: true
    }
});

module.exports = mongoose.model('BusinessCard', BusinessCardSchema);

您應該從名片中刪除 _id。 如果不這樣做,則會出現重復錯誤。 喜歡 :

 exports.updateBusinessCard = async(req, res) => { try { const { name, title, email, phone, website, bName, bServ } = req.body; let businessCard = await BusinessCard.findById(req.params._id); if (!businessCard) { res.status(404).json({ msg: 'Business card not found' }); } businessCard.name = name; businessCard.title = title; businessCard.email = email; businessCard.phone = phone; businessCard.website = website; businessCard.bName = bName; businessCard.bServ = bServ; delete businessCard._id; // ADD THIS LINE // findByOneAndUpdate // findByIdAndUpdate businessCard = await BusinessCard.findByOneAndUpdate({ _id: req.params._id }, businessCard, { new: true }) res.json(businessCard); } catch (error) { console.log(error); res.status(500).send('Server error: Update business card!'); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM