Unable to locate the control specified by the path: 'files -> 0 -> postId'

I am in the process of creating a dynamic form with formArray and running into an issue. When I click on the AddItem button, it should create an input field for uploading files.

Here is the HTML code snippet I am using:

<div class="row m-auto col-md-12 pb-4">
    <div class="col-md-2 col-xs-2 col-sm-2 col-lg-2 col-xl-2">
        <button (click)="AddItem()" mat-stroked-button color="warn">
            <i class="la la-plus"></i>
            <label class="pr-2 pl-2">
                Create New File
            </label>
        </button>
    </div>
</div>
...

and this is the code in my TypeScript file:


@Input() private postId: number;

uploadFormGroup: FormGroup;
fileType = TypeFile;
fileUpload: FileUpload;

constructor(private formBuilder: FormBuilder, private postService: PostService) { }

ngOnInit(): void {
  this.uploadFormGroup = this.formBuilder.group({
    postId: [''],
    files: this.formBuilder.array([]),
    typeEnum: ['', Validators.compose([Validators.required])]
  });
}

...

However, when I try to add a new item by clicking on the AddItem() button, I encounter the following error:

Cannot find control with path: 'files -> 0 -> postId'

What could be causing this problem?

Answer №1

Please review your code carefully. The postId control is located within the uploadFormGroup, not under the files formArray as you are trying to access it there.

You have two options: either add postId in the files formArray, or access it outside of the formArray loop in your HTML.

createItem(): FormGroup {
 return this.formBuilder.group({
  file: new FormControl(),
  postId: new FormControl()
 });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 2: Creating a Reusable Object for Uniform JSON Structures

I am facing an issue with JSON data as I have 3 tables in my database named "dictionary" with the same structure but different column names: {"id":1,"test_1":"test"},{"id":2,"test_1":"lalala"} - first JSON {"id":1,"test_2":"****"},{"id":2,"test_2":"afe ...

Add the file retrieved from Firestore to an array using JavaScript

Trying to push an array retrieved from firestore, but encountering issues where the array appears undefined. Here is the code snippet in question: const temp = []; const reference = firestore.collection("users").doc(user?.uid); firestore .collec ...

How can I utilize passed in parameters in Meteor React?

I am trying to figure out how to use two params that I have passed in the following example. Can someone please assist me? updater(layer, item){ this.setState({layer5: <img id="layer5" className="on-top img-responsive center-block" name="layer5" ...

Step-by-step guide on integrating a specific location into Google Maps using React.js

I'm in the process of revamping my website using Reactjs. I want to incorporate a specific Google location with reviews on the map, similar to how it appears on this example (My current website is built on Wordpress). As of now, all I've been ab ...

Using arrays to pass parameters in JavaScript

I have multiple sliders that require the same set of parameters to be passed to each one. My understanding is that I need to use an array for this purpose. However, as a JavaScript novice, I am unsure of the correct way to implement it. The parameters lo ...

Determine the Button's State by Monitoring Changes in the TextBox Text

I have been tasked with developing a web application for my company. The main components of the application are a button and a textbox. My goal is to allow users to input a value into the textbox, which will then be processed when they click on the button ...

Restrict page scrolling to the vertical position of a specified div [Using jQuery, javascript, and HTML]

Currently, I am in the process of developing a personal website that features numerous expandable items. My goal is to restrict the page scrolling to the height of the expanded item once it is opened. I do not want users to be able to scroll above or below ...

What is the best way to eliminate the first even number from a string?

My task involves working with a string containing only numbers. For example: let inputString = "1234"; The Challenge I need to create a function that will return the string excluding the first even number, if one exists. Example Output: " ...

Do I need to be concerned about a bot attack if my form does not submit to a specific endpoint in a single-page application (SPA)?

My latest project involves a form with fields for email and password, but instead of POSTing the data, it simply calls a function upon submission. Although I'm using Angular, I wonder if I should be worried about potential bot attacks. Do you think I ...

What is the best way to automatically log out a user once their session cookie has expired during an online exam?

While developing an MVC ExpressJS Test app, I encountered an issue with auto-logout functionality for users who stop answering test questions. The error message "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" keeps app ...

How to Send Javascript DOM Value to Inner HTML

I am trying to extract a value from a hidden element ID and set it as the inner HTML for another element ID. Below is my code: <script> function replaceText(){ var x=document.getElementById("mainTitle1").textContent; document.getElementById("mainTi ...

Having trouble with [Object Object] errors in your hybrid app using Javascript?

I'm currently developing a hybrid app using Nuxt JS, Cordova, and Cordova Native Storage (essentially localstorage). During the process of saving an object to native storage and retrieving it on page load within the mounted() method, I keep encounter ...

Exploring the features of the window object in an Angular application compiled ahead of time

In an effort to streamline our development process and avoid having to build the Angular-App multiple times for different environments, we decided to skip injecting environment-specific variables (such as service endpoints) into our app using regular file ...

Switching classes in jQuery for Internet Explorer 8

I am attempting to update the color of a header when it reaches a certain scroll position. I have implemented this script using jQuery: var $document = jQuery(document), $element = jQuery('#header'), className = 'red'; $docume ...

I am not sure where the file is stored between selecting it for upload and actually uploading it

Currently, I am developing a web application using C#, Angular, and SQL. I am in the process of creating a feature that allows users to upload an Excel file into the database by clicking a button on the frontend. Upon clicking the button, a dialog box open ...

Error in scrolling previews detected in Jssor horizontal template maker

I've been working with Jssor Slider Maker and I'm using the vertical preview template that features two columns on the left side and a maximized image on the right side. After pulling the code from the developers pack, it includes scripts, CSS an ...

Vertical Positioning of Tabs in Materialize CSS

My current challenge involves creating vertical tabs using materialize CSS, specifically in regards to positioning. The desired outcome is for the content to align at the same level when clicking on TAB 3. An example of the expected result can be seen in t ...

Unveiling the secret to accessing properties using v-if within a custom component template relocation

I'm currently working on an application that reveals a hidden div when the text "Create Test" is clicked. Everything works well when the code isn't placed inside the template of a component. This seems strange to me, what could be causing this is ...

Securing page access with a straightforward password protection using Flask

Looking for a straightforward way to add password protection to a Flask app site. I've explored options like flask_login, but they seem overly complex for my needs. I don't need high security or login sessions - just something like: "enter passw ...

Using Angular to parse intricate JSON data

Need help parsing an http request in the following format: [ { "id": 1, "date": "2022-01-13T00:00:00.000+00:00", "time": "2022-01-13T21:21:21.000+00:00", "office&quo ...