node.jsangularmongoosehttpclientangular-httpclient

Postman POST call works Angular doesn't


I have the following problem: I am trying to make a mew record in a mongoDB database using a call from an Angular frontend. For some reason when i initiate the POST request i dont get any kind of error message but the record doesnt get created either.

Using POSTMAN to make the request it does create the record.

My code for backend looks like this:

const express = require('express');
const router = express.Router();
const Munkafolyamat = require('../models/munkafolyamat');

router.post('/addmunka', async (req, res) => {
    console.log('made it to backend')
    const munka = new Munkafolyamat({
      title: req.body.title,
      description: req.body.description,
      completed: req.body.completed
    });
    try {    
      const newMunka = await munka.save();
      res.status(201).json(newMunka);
    } catch (error) {
      res.status(400).json({ message: error.message });
    }
  });

for the frontend i have an apiService making this call:

createMunka(munka: any): Observable<any> {
    return this.http.post(`${this.apiUrl}/munkas/addmunka`,munka);
  }

and lastly i call this method of the apiService in my component:

save(){
      this.teendo.elvegezte='jani';
      this.teendo.todo='megkőcsináni';
      this.teendok.push(this.teendo)
      this.munka.inditotta=this.inditotta;
      this.munka.iroda=this.iroda;
      this.munka.tipus=this.tipus;
      this.munka.eszkozlista=this.eszkozlista;
      this.munka.status=this.status;
      this.munka.teendok=this.teendok;
      
      console.log('munka: ', this.munka);
      this.apiService.createMunka(this.munka)
    }

also dont know if its important or not but the mongoose schema looks like this:

const mongoose = require('mongoose');

const munkafolyamatSchema = new mongoose.Schema({
    tipus:String,
    inditotta:String,
    iroda:String,
    status:String,
    eszkozlista:[String],
    teendok:[{elvegezte:String,todo:String}]

  });
  module.exports = mongoose.model('Munkafolyamat', munkafolyamatSchema, 'Munkafolyamat');

Solution

  • The method from the service is an observable, you have to subscribe to the observable (to execute the API call) and inside the callback of next you will get the response, where you can show a success toaster message and inside the error callback, where you will get the error returned you can show an alert for the same or other error handling logic.

    save(){
          this.teendo.elvegezte='jani';
          this.teendo.todo='megkőcsináni';
          this.teendok.push(this.teendo)
          this.munka.inditotta=this.inditotta;
          this.munka.iroda=this.iroda;
          this.munka.tipus=this.tipus;
          this.munka.eszkozlista=this.eszkozlista;
          this.munka.status=this.status;
          this.munka.teendok=this.teendok;
          
          console.log('munka: ', this.munka);
          this.apiService.createMunka(this.munka).subscribe({
            next: (response: any) => {
              console.log(response);
            },
            error: (error: any) => {
              console.log(error);
            },
          }) // <- changed here!
    }