The username property in Angular is being read as undefined and causing an error

Currently, I am in the process of implementing a "Create" function to generate appointments and also include the User ID of the creator. To achieve this, I have set up models for "rendezvous" and "user." However, upon executing the project, I am encountering an error in the console:

core.mjs:7643 ERROR TypeError: Cannot read properties of undefined (reading 'id') at CreateRendezvousComponent_Template (create-rendezvous.component.html:45:9) at executeTemplate (core.mjs:12114:9) at refreshView (core.mjs:11977:13)

The error seems to be originating from the HTML of the "create-rendezvous" component:

        <label>Etat</label>
        <input type="text" class="form-control" id="etat"
        [(ngModel)]= "rendezvous.etat" name="etat">
    </div>
   <div class="form-group">
        <label>User ID</label>
        <input type="number" class="form-control" id="user.id"
        [(ngModel)]= "rendezvous.user.id" name="user.id">
    </div>
     <button class="btn btn-success" type="submit">Add</button>

</form>

</div>

This is the "Create" function within the "create-rendezvous" component:

saveRendezVous(){
    this.rendezvousService.createRendezvous(this.rendezvous).subscribe(data=>{
         console.log(data);
         this.goToRendezvousList();
        
    }, 
    error=>console.log(error));
  }

And here is the function in the Angular service:

createRendezvous(rendezvous:Rendezvous): Observable<Object>{
    return this.httpClient.post(`${this.baseUrl}`, rendezvous);

  }

Your assistance with this matter is greatly appreciated.

Answer ā„–1

When you encounter 'undefined', it indicates that you are attempting to access the 'id' property of an object that has not yet been set up. Ensure that both your rendezvous object and its user object are not only defined, but also initialized.

Also, make sure that the user object itself is defined. If its definition depends on data loaded through an API call, consider implementing a loading state to handle this delay.

Answer ā„–2

Consider incorporating the Safe navigation operator into your code, specifically in situations involving the user.id attribute.

For instance, user?.id

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

Sending data from view to controller in Angular using TypeScript

I am currently working with AngularJS and TypeScript, and I have encountered an issue with passing a parameter from the view to the controller. In my attempts to solve this problem, I have utilized ng-init as follows: <div class="col-md-9" ng-controlle ...

Error message thrown when attempting to execute foundation() function in Karma Unit Test for Angular 5 with Foundation integration

After upgrading my angular4 project to angular 5, I encountered a problem with the Foundation modal. While the modal opens as intended when running the app, running unit tests with Karma results in the following error: Failed: $(...).foundation is not a f ...

After deploying the application, the 'Access-Control-Allow-Origin' setting was not found

After deploying the same application to 2 servers (one for testing and one for production) on IIS, I encountered an issue. The application works perfectly on the first server, but not on the second one. While I am able to connect, retrieve data, and add ...

Defining the range of an array of numbers in TypeScript: A complete guide

When working with Next.js, I created a function component where I utilized the useState hook to declare a variable for storing an array of digits. Here is an example: const [digits, setDigits] = useState<number[]>(); I desire to define the range of ...

Can you generate two distinct arrays by clicking create?

My website has a customer page called customer.html: <app-z-grid title="Tip korisnika" [restPath]="'customertype'" (fillDetailParent)="reinit($event)"></app-z-grid> <app-z-grid title="Podtip korisnika" [path]='"custome ...

What are some methods for utilizing spring security Ldap to authenticate with angularjs on the client side?

Recently, I developed a web application using spring-mvc and angularjs. Initially, I implemented authentication by storing user credentials in a database table. However, I now have a requirement to switch to LDAP for authentication. Can anyone provide guid ...

Reactive forms in Angular do not refresh or update automatically when a new error is added

Upon initializing my FormGroup in the ngOnInit() method, I invoke a validator function to ensure that the password and confirmPassword fields match. This is how it looks in TypeScript: regForm: FormGroup; constructor() { } ngOnInit() { this.regFo ...

What are the steps for personalizing themes in the Monaco editor?

I'm currently working on a code editor with Monaco. The syntax highlighting in Monaco for Javascript and Typescript only highlights keywords as dark blue, strings as brown, and numbers as light greenish-yellow. My goal is to customize the vs-dark the ...

Error: Exceeded Maximum Re-Renders. React has set a limit on the number of renders to avoid infinite loops. Issue found in the Toggle Component of Next.js

I am struggling with setting a component to only display when the user wants to edit the content of an entry, and encountering an error mentioned in the title. To achieve this, I have utilized setState to manage a boolean using toggle and setToggle, then ...

The object assigned in the Facebook login method cannot be utilized

I'm working on integrating Facebook login with Angular 2. Here's the button for logging in: <button ion-button (click)="fbLogin()"><ion-icon name="logo-facebook"></ion-icon>Login using Facebook</button> Below is the clic ...

What is the best way to specify the return type in TypeScript when there is a possibility of multiple types?

I'm currently working on implementing type definitions for an Axios API client but Iā€™m struggling with indicating to the compiler the specific return type that I am expecting. Here is a simplified example: import axios, { AxiosResponse } from "axi ...

The sequence of output in TypeScript when using Gulp is similar to running tsc with a tsconfig

After setting up a tsconfig file and successfully running the command-line tsc, I encountered an issue when using gulp-typescript with a tsconfig.json and outFile specified. The output ordering was different, and I have been unable to find a solution in Gu ...

Creating an OpenLayers map within an Ionic component

Implementing Ionic v4 with OpenLayers v5.3 I am facing an issue while trying to display an OL map as a component in my project. Only the icons are being loaded and it seems like the styles and JS configuration are not loading properly. I have tried inject ...

Timeout with Promise

I'm looking to enhance my understanding of working with promises by rewriting this function to resolve the promise instead of resorting to calling the callback function. export const connect = (callback: CallableFunction|void): void => { LOG.d ...

"Encountered an issue: Error occurred while attempting to synchronize Protractor with the page" during the execution of Protractor tests

I am facing an issue while running Protractor tests on a web application that includes both Angular and non-angular elements. Here is the structure of my code: describe("Test Name", function() { it("Test case", function() { // starting with steps on ...

The latest version of Angular (7.0.5) introduces a new feature where the ng new command no longer prompts the user with the question "Would

I'm looking for assistance in figuring out why I am unable to create a new project using the latest version of Angular 7. Desired outcome: ng new firebase-auth ? Would you like to add Angular routing? No ? Which stylesheet format would you like to u ...

Is there a source where I can locate type definitions for Promise objects?

In the process of creating a straightforward class called Primrose, I am extending the global Promise object in order to include additional methods like resolve and reject. export class Primrose<Resolution> extends Promise<Resolution>{ priv ...

How can main component be hidden or removed from view on specific routes when changing pathways?

In the html template located in my app.component.html file, I have the following code ` <app-layout> <mat-sidenav-container> <mat-sidenav #sidenav role="navigation"> <app-sidenav-list (sidenavClose)="sidenav.cl ...

"Upon studying the Reflect-metadata index.d.ts file, I find myself perplexed by the variances in

While utilizing the reflect-metadata package, I encountered this particular type. However, I am uncertain about its meaning and function signature. function metadata(metadataKey: any, metadataValue: any): { (target: Function): void; ( ...

Updating the button text in Angular 7

Here's a question: <button (click)="activateMotion(1)"> <img class="emotion-icon" id="positive-icon" src="" /> </button> <button (click)="activateMotion(-1)"> <img class="emotion-icon" id="negative-icon" src="" /&g ...