0

I have a reactive form I want to be able to push to an api, but each time I submit I get "Error! TypeError: "cyclic object value".

From my research I understand that this is because somewhere my Object it's referencing itself and JSON can't handle that. I think I found where I have the object reference itself (marked with an arrow) but I don't understand how can I avoid it.

HTML Form

 <form [formGroup]="projectForm" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <input type="text" id="nameInput" formControlName="projectName" placeholder="Project Name">
      </div>
      <div class="form-group">
        <input type="text" id="languageTags" formControlName="projectLanguage" placeholder="Language Tags">
      </div>
      <div class="form-group">
        <input type="text" id="githubLink" formControlName="projectLink" placeholder="GitHub Link">
      </div>
      <div class="form-group">
        <input type="text" id="initDate" formControlName="projectDateInit" placeholder="Init Date & time">
      </div>
      <div class="form-group">
        <textarea type="text" id="nameInput" formControlName="projectDetails" placeholder="Details"></textarea>
      </div>
      <div class="btnContainer">
        <button class="btn btn-primary btnMarginLeft" (click)="closeForm()">Cancel</button>
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </form>

Form Component

import { ServerControlService } from './../server-control.service';
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';


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

  projectForm: FormGroup;                       <--- Is this where I am making a circular object?

  constructor(private _serverControl: ServerControlService) { }

  ngOnInit() {
    this.projectForm = new FormGroup({
      projectName: new FormControl(),
      projectLanguage: new FormControl(),
      projectLink: new FormControl(),
      projectDateInit: new FormControl(),
      projectDetails: new FormControl()
    });
  }

  @Output() closeFormEvent = new EventEmitter();

  closeForm() {
    this.closeFormEvent.emit();
  }
  onSubmit(): void {
    this._serverControl.projectInit(this.projectForm)
      .subscribe(
        data => console.log('Success', data),
        error => console.error('Error!', error)
      )
  }
}

Server Service Component

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ServerControlService {

  _url = '/assets/data/projectsDatabase.json';

  constructor(private _http: HttpClient) { }

  projectInit(projectData) {
    return this._http.post<any>(this._url, projectData);
  }

}

1 Answer 1

3

You should just do this this._serverControl.projectInit(this.projectForm.value) instead of this this._serverControl.projectInit(this.projectForm)

this.projectForm.value contains an object with the same structure as you defined, with the values for every field. this.projectForm contains the form behavior and is not meant to be serialized.

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

Comments

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.