Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - Uncaught (in promise): TypeError: Cannot read property 'title' of undefined

Tags:

angular

I see this error in my App when I try to add some text input field with dual binding:

   ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'title' of undefined
TypeError: Cannot read property 'title' of undefined
    at Object.eval [as updateRenderer] (ng:///AppModule/TapesComponent.ngfactory.js:660:29)
    at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12658:21)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12037:14)
    at callViewAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12347:17)
    at execComponentViewsAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12293:13)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12038:5)
    at callWithDebugContext (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:13020:42)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12560:12)
    at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:10129:63)
    at RouterOutlet.activateWith (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:5378:42)
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4558:16)
    at ActivateRoutes.activateRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4539:26)
    at eval (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:58)
    at Array.forEach (native)
    at ActivateRoutes.activateChildRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:29)
    at Object.eval [as updateRenderer] (ng:///AppModule/TapesComponent.ngfactory.js:660:29)
    at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12658:21)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12037:14)
    at callViewAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12347:17)
    at execComponentViewsAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12293:13)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12038:5)
    at callWithDebugContext (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:13020:42)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12560:12)
    at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:10129:63)
    at RouterOutlet.activateWith (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:5378:42)
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4558:16)
    at ActivateRoutes.activateRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4539:26)
    at eval (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:58)
    at Array.forEach (native)
    at ActivateRoutes.activateChildRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:29)
    at resolvePromise (http://localhost:3000/node_modules/zone.js/dist/zone.js:712:31) [angular]
    at resolvePromise (http://localhost:3000/node_modules/zone.js/dist/zone.js:683:17) [angular]
    at http://localhost:3000/node_modules/zone.js/dist/zone.js:760:17 [angular]
    at Object.onInvokeTask (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:4123:37) [angular]
    at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:397:36) [angular]
    at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:165:47) [<root> => angular]
    at drainMicroTaskQueue (http://localhost:3000/node_modules/zone.js/dist/zone.js:593:35) [<root>]
    at HTMLAnchorElement.ZoneTask.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:464:25) [<root>]

I have this Tape class:

    export class Tape {
  constructor (
  public id: number,
  public title: string,
  public date?: string,
  public rating?: number,
  public description?: string,
  public photo?: string){}
}

And component

import { Component, OnInit } from '@angular/core';
import { Tape } from './tape'
import { TapeService } from './tape.service';
import { Router } from '@angular/router';
// import { TitlePipe } from './titleFilter';

@Component({
  selector: 'my-tapes',
  templateUrl: './tapes.component.html',
  styleUrls: [`./tapes.component.css`],
  providers: [TapeService],

})
export class TapesComponent implements OnInit {
  pageName = 'VHS Movies listing';
  tapes: Tape[];
  selectedTape: Tape;
  model = new Tape(0,'s','s',0,'s','sS'); 

Component HTML looks like

<div class="form-group">
              <label for="title">Title{{model.title}}</label>
              <input type="text" class="form-control" id="title" required [(ngModel)]="model.title" name="title" #title="ngModel">              Written text: {{tape.title}}
              <div [hidden]="title.valid || title.pristine" class="alert alert-danger">
                Title is mandatory
              </div>
            </div>

I just started learning Angular 2, but for me it should get title from module. He he can't read title?

like image 516
gosia koduje Avatar asked Apr 08 '17 10:04

gosia koduje


3 Answers

Try to use the safe /elvis operator to check if the values are present then display,

 <input type="text" class="form-control" id="title" required [(ngModel)]="model?.title" name="title" #title="ngModel"> Written text: {{model?.title}}
like image 148
Sajeetharan Avatar answered Jan 04 '23 12:01

Sajeetharan


Rather than using the Elvis operator, what I did was to define the object as an empty one at first then assigning the needed value via service call.

model = <model>{}
//here goes service call
like image 44
Partha Roy Avatar answered Jan 04 '23 13:01

Partha Roy


You are right:

Written text: {{tape.title}}

should be

Written text: {{module.title}}

Stupid mistake! Thank you!

like image 20
gosia koduje Avatar answered Jan 04 '23 13:01

gosia koduje