0

I am creating a completely new object through the Angular 5 reactive forms, when it has been filled up. The forms however, do not have any id elements. The question is, when i click submit, I get the form.value. When i console.log it, i can see the JavaScript object of form.value. How do i submit this to a web API? The JavaScript object does not have any id too.

Please provide suggestions or the methodology in doing so.

I am using the new HTTP Client module.

Thank you.

Here are my source codes:

.ts

  CheckListForm: FormGroup;

  Ques: Questions[];

  Questions: any = [];

  employmenttype = ['Permanent', 'contractor'];



  constructor(private fb: FormBuilder,
          private checklistservice: ChecklistService) { 
          this.CreateForm();

                                                       }

  ngOnInit() {

  this.checklistservice.getQuestions(1).subscribe(res =>{ this.Ques = res;
  this.addQuestions();
             }); 








 CreateForm() {
 this.CheckListForm = this.fb.group({

 name: ['', Validators.required],
 EmploymentType: ['', Validators.required],
 HRMS: [''],
 CompanyName:[''],
 questions: this.fb.array([]) 
              })
             }


get questions(): FormArray {
return this.CheckListForm.get('questions') as FormArray;
                           }


addQuestions () {

  for (let Que of this.Ques) {
    this.questions.push(
      new FormGroup ({
        'ques': new FormControl(Que.ques),
        'choices': new FormControl('', Validators.required)
      })
    )
  }
}


onSubmit() {
  console.log(this.CheckListForm.value);
           }
         }

And here is my console.log(this.checklistform.value) results:

enter image description here

3
  • 1
    Can you share your code which you have tried so far Commented May 11, 2018 at 9:09
  • You need to iterate through the javascript object and take the values of the controls that you want to use. Its really hard to answer a question when you don't share any of the code, console logs or anything else. Can you put some code and the console log of your form value? What is your POST request expecting? Commented May 11, 2018 at 9:09
  • I have added the codes for the reactive form controls, together with the json object. As you can see, the json object contains an array questions, with 2 form controls, ques and choices. Commented May 11, 2018 at 9:25

1 Answer 1

1

You can generate the id in the API before it writes to database. Or you can create the id in service and add it to the POST request body before posting to API.

So it would be something along these lines in the service:

addItemsToDB(arrayOfObjects) {
    let array = arrayOfObjects;
    array.forEach(object => {
        object.id = uuid();
    });
    return this.http.post(this.databaseURL, array);
}

First install uuid (https://www.npmjs.com/package/uuid) via npm

npm i uuid

Then import it like this

import { v4 as uuid } from 'uuid';

Then you can generate uuids with just calling uuid(); More info about what is an uuid: https://en.wikipedia.org/wiki/Universally_unique_identifier

I hope this helps! :)

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

7 Comments

Thank you for the response. But the method uuid(); how does it generate an id. Could you kindly explain further? How to ensure the id does not conflict with an existing id record in the database?
Thank you so much for the response. I will try this awesome feature out!
Hi sorry, just one more question, how do i add the ids for the array objects ?
Edited answer again :) Not sure if this is what you are really looking for, but if the API can work with arrays, then this should work.
dude, you are a life saver if this works. I will carry out the codes and see if it works. I will keep you posted also for learning purposes. Thanks for sharing this awesome uuid
|

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.