question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NgbTimepicker - When meridian is true: error Cannot read property 'hour' of undefined

See original GitHub issue

Under timepicker.ts you’ll see that model isn’t initialized, only declared. Model is essentially undefined until angular2 initiates the binding with ControlValueAccessor and writeValue() is called.

export class NgbTimepicker implements ControlValueAccessor,
    OnChanges {
  disabled: boolean;
  model: NgbTime;

I’m not sure why it’s working in your demo, however I can only assume that something has changed between versions and this fix will be needed as I’m on the latest (2.2.0) Also doesn’t work with (2.0.2).

If you’ll notice below that we don’t hold model accountable when trying to access parameters from it until we get to checking if meridian is true.

            <input type="text" class="form-control" maxlength="2" size="2" placeholder="MM"
              [value]="formatMinSec(model?.minute)" (change)="updateMinute($event.target.value)" 
              [readonly]="readonlyInputs" [disabled]="disabled">
          </td>
          <template [ngIf]="seconds">
            <td>&nbsp;:&nbsp;</td>
            <input type="text" class="form-control" maxlength="2" size="2" placeholder="SS"
              [value]="formatMinSec(model?.second)" (change)="updateSecond($event.target.value)" 
              [readonly]="readonlyInputs" [disabled]="disabled">
          </template>
          <template [ngIf]="meridian">
            <td>&nbsp;&nbsp;</td>
            <td>
              <button type="button" class="btn btn-outline-primary" (click)="toggleMeridian()">{{model.hour >= 12 ? 'PM' : 'AM'}}</button>

I believe we need to soak up the undefined reference by treating the model as possibly undefined if the meridian is set to true. Currently we’re expecting there to be an hour key on a possibly defined object and that doesn’t work since model isn’t set on class initiation, only when a change to the model is made and detected through controlvalueaccessor.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
kumarlalapetcommented, Apr 26, 2017

I started facing the same error under the following conditions

  1. Component is under a form tag hence provided name attribute for the ngb-timepicker
  2. Meridian is true

Worked for me when meridian is true and NOT under form tag and no name attribute was provided in the ngb-timepicker component. Not sure if this helps.

My workaround now is using [ngModelOptions]=“{standalone: true}” so this is not processed by form.

1reaction
esistgutcommented, Jul 21, 2017

http://plnkr.co/edit/5pQE9DcqgcpJDmpPUI0f this plunker seems to correctly show the issue. Just change [meridian] to false and errors should no longer appear. Should I open another issue for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

error Cannot read property 'hour' of undefined - Bountysource
NgbTimepicker - When meridian is true : error Cannot read property 'hour' of undefined.
Read more >
Why am I getting this error using ngModelChanges in my ...
The functionality works just fine, so the second time it runs through the (ngModelChange) it declares startTime and endTime as null.
Read more >
Timepicker - Angular powered Bootstrap
Component. A directive that helps with wth picking hours, minutes and seconds. Selector: 'ngb-timepicker'.
Read more >
Task processing fails with error Cannot read property length
TypeError : Cannot read property 'length' of undefined. It looks like the files all process until the end of the video then fail....
Read more >
@ng-bootstrap/ng-bootstrap | Yarn - Package Manager
... timepicker: add exportAs property for ngbTimepicker (#3980 (94e7b35) ... timepicker: display 12 PM/AM when meridian is true (91ca518), closes #1031 # ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found