Error in Angular Material Date Picker: Property of undefined cannot be read

I am attempting to utilize a MatDatePicker in my project, but I keep encountering some unusual errors that are visible in the console (Screenshots provided below). It seems like I am unable to open the date picker.

View Console error 1

View Console error 2

Below is the relevant code for reference:

App Module

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MatIconModule,
    MatButtonModule,
    FlexLayoutModule,
    MatDatepickerModule,
    MatInputModule,
    FormsModule,
    ReactiveFormsModule,
    MatNativeDateModule,
  ],
  providers: [MatDatepickerModule, MatNativeDateModule],
  bootstrap: [AppComponent],
})

App Component

export class AppComponent {

  // Default start date set to today
  myDate = new FormControl(new Date());

  constructor() {}

  ngOnInit(): void {}
}

App Component HTML

<mat-form-field appearance="outline">
  <mat-label>Select a start date</mat-label>
  <input
    matInput
    [matDatepicker]="picker"
    [formControl]="myDate"
    />
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-date-picker #picker></mat-date-picker>
</mat-form-field>

Package.json

  "dependencies": {
    "@angular/animations": "~13.0.0",
    "@angular/cdk": "^13.0.0",
    "@angular/common": "~13.0.0",
    "@angular/compiler": "~13.0.0",
    "@angular/core": "~13.0.0",
    "@angular/elements": "^13.0.0",
    "@angular/flex-layout": "^12.0.0-beta.35",
    "@angular/forms": "~13.0.0",
    "@angular/material": "^13.0.0",
    "@angular/platform-browser": "~13.0.0",
    "@angular/platform-browser-dynamic": "~13.0.0",
    "@angular/router": "~13.0.0",
    "document-register-element": "^1.7.2",
    "rxjs": "~6.6.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },

The page is rendering without any issues,

Correctly rendered field can be viewed here

However, despite the correct rendering of elements, the console still displays errors as mentioned earlier. Additionally, clicking on the date picker toggle triggers another error:

Error: datepicker.open not a function

I'm unsure where I might have gone wrong with this setup. Any insights or suggestions would be greatly appreciated.

Answer №1

Big thanks to @Carsten for providing the correct answer. Just a small typo!

This is what I initially had:

<mat-date-picker #picker></mat-date-picker>

And here's the corrected version:

<mat-datepicker #picker></mat-datepicker>

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

How to utilize ngFor for multiple inputs in Angular 4

Apologies for my poor English, I'll do my best to explain my issue. Here's the problem: I'm using ngFor on an Input element, but when I enter data, it gets repeated in all the fields and I can't figure out why. <div *ngFor=" ...

Encountered an npm installation error: The rollbackFailedOptional issue arose during the npm session with the verb npm-session a0d68 while attempting the command npm install --save

Every time I attempt to install babel, I encounter the following error message: rollbackFailedOptional: verb npm-session a0d68 The version of Node currently installed is 12.16.1 The version of npm being used is 6.13.4 All I want is to get babel up and ...

the undefined 'pipe' cannot be read

Trying to perform unit testing for an Angular component is a new experience for me. Currently, I am encountering a specific issue that I would like assistance with. The component in question contains the following select statement: this.store.select(getI ...

Using AngularJS to encapsulate the JSON response received from the server

Currently, I have a basic CRUD application that is operational. However, I am looking to enhance every response received from the server by adding two additional parameters: 'error' => boolean, 'errorMessage' => string, 'dat ...

Using JavaScript and jQuery to create a delay when handling input events

I am working on a project where I have an HTML table that retrieves its values from a database using jQuery Ajax. Here is an example: <div id="tableId"></div> Below is the JavaScript code: function showUser(rowsToShow) { request = $.get("s ...

Adjusting the gap between TableRows in Material-UI

Is there a way to increase the spacing between TableRow MaterialUI components in my code? <S.MainTable> <TableBody> {rows.map(row => { return ( <S.StyledTableRow key={row.id}> <TableCell component="th" s ...

Using Ajax with Angular services

Recently, I decided to explore Angular JS and Async calls as an alternative to $JQuery. However, I encountered a peculiar issue while making ajax calls. To adhere to 'best practices', I shifted my ajax requests to a service and then initiated th ...

Discovering the current active link and anchor tag details using jQuery in an unordered list

I am currently utilizing jQuery to work with 4 anchor tags inside an unordered list, and I would like to be able to determine the "current active link" when a user performs a search. This way, I can execute my query based on the selected anchor tag. How ca ...

The DELETE Request is unable to parse the ID from the URL

I am currently working on a MEAN Stack application and I am running into an issue while trying to implement a delete functionality for reservations. router.delete('/api/reservaties/:reservatie', auth, function (req, res, next) { Reservatie.r ...

What is the best way to incorporate a third-party element into Vue using a script tag?

I am in the process of developing a website and I would like to include a widget that links to a podcast on BuzzSprout. Initially, I created the site using HTML to test out different designs, but now I am looking to transition it to VueJS. In my HTML vers ...

Is it possible to integrate the Pug template with the Angular builder using esbuild?

Currently in my project using Angular 17, I have set the builder as "@angular-builders/custom-webpack:browser". Now, I am interested in switching to esbuild for the builder. However, my project currently relies on pug templates instead of html. Is it possi ...

Access all the properties of an object within a mongoose record

My database contains a collection of documents that are structured using the mongoose and express frameworks. Each document follows this schema: const userSchema = new Schema({ firstName: { type: String }, lastName: { type: String }, email: { t ...

Is it acceptable to employ async: false when requesting small amounts of data?

I am curious about the best practices for retrieving small data from the server. One example is using an ajax (or sjax) call to check for new notifications for a user. function checkNewNotifs() { $.ajax({ url: '/Home/CheckNewN ...

Is there a way to ensure that the vertical scrollbar remains visible within the viewport even when the horizontal content is overflowing?

On my webpage, I have multiple lists with separate headers at the top. The number of lists is dynamic and can overflow horizontally. When scrolling horizontally (X-scrolling), the entire page moves. However, when scrolling vertically within the lists, I wa ...

Error: Template Update Required Due to Observable Value Change - Fixing the ExpressionChangedAfterItHasBeenCheckedError

After searching through numerous SO posts in search of a solution, I finally stumbled upon one that involved a hacky implementation. The issue at hand involves an observable extracted from the ngrx store which I then subscribe to: this.navigationSelected$ ...

Encountering the "JavaScript heap out of memory" error is a frequent occurrence when running npm scripts

Encountering issues with running npm and seeing the error message FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory. This problem arose after installing node.js&npm on my new PC. The only npm command that functions pro ...

The embedded Twitter widget in the Angular 2+ app is visible only upon initial page load

After implementing the built-in function from Twitter docs into ngAfterViewInit function, my app works flawlessly. However, I encountered an issue where the widget disappears when switching routes. Here is the code that only functions on the initial page ...

What is the best method for creating a 10-page PHP form that contains more than 100 input fields in an efficient manner?

I am currently in the process of constructing a substantial PHP form that will extend over 10 pages and contain well over 100 input fields. Each input will be stored in a database. I am starting to encounter challenges keeping track of all the variables as ...

There was an issue retrieving the user with a particular email and status in mongodb

I am trying to retrieve a user order with a specific email and status that is not equal to "Order Completed". However, I am encountering an error stating "Expression $ne takes exactly 2 arguments. 1 were passed in." It would be greatly appreciated if someo ...

Can you guide me on utilizing filter in an Apps Script array to retrieve solely the row containing a particular user ID within the cell?

I am using an Apps Script that retrieves data from four different columns in a spreadsheet. However, it currently fetches all the rows instead of just the row that matches the randomly generated 8-digit user ID. function doGet(req) { var doc = Spreadshe ...