ERROR Typeerror: Cannot read Property 'push' of null

Asked

Viewed 3,915 times

1

I am trying to push to a list and the following error is occurring:

ERROR TypeError: Cannot read property 'push' of null
    at ProjectService.webpackJsonp.../../../../../src/app/projects/shared/project.service.ts.ProjectService.createItem (project.service.ts:34)
    at ProjectFormComponent.webpackJsonp.../../../../../src/app/projects/project-form/project-form.component.ts.ProjectFormComponent.createItem (project-form.component.ts:20)
    at Object.eval [as handleEvent] (ProjectFormComponent.html:8)
    at handleEvent (core.es5.js:12047)
    at callWithDebugContext (core.es5.js:13508)
    at Object.debugHandleEvent [as handleEvent] (core.es5.js:13096)
    at dispatchEvent (core.es5.js:8659)
    at core.es5.js:9270
    at HTMLButtonElement.<anonymous> (platform-browser.es5.js:2668)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)

The code of the Office:

import { Injectable } from '@angular/core';
// import { AngularFire, FirebaseListObservable, FirebaseObjectObservable, AngularFireDatabase } from "angularfire2";
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { Project } from './project'

@Injectable()
export class ProjectService {

  private basePath: string = '/projects';

  items: FirebaseListObservable<Project[]> = null; //  list of objects
  item: FirebaseObjectObservable<Project> = null; //   single object

  constructor(private db: AngularFireDatabase) {}

  // Return an observable list with optional query
  // You will usually call this from OnInit in a component
  getItemsList(query={}): FirebaseListObservable<Project[]> {
    this.items = this.db.list('/projects', {
      query: query
    });
    return this.items
  }

  // Return a single observable item
  getItem(key: string): FirebaseObjectObservable<Project> {
    const itemPath =  `${this.basePath}/${key}`;
    this.item = this.db.object(itemPath)
    return this.item
  }

  // Create a bramd new item
  createItem(item: Project): void  {
    this.items.push(item)
      .catch(error => this.handleError(error))
  }


  // Update an exisiting item
  updateItem(key: string, value: any): void {
    this.items.update(key, value)
      .catch(error => this.handleError(error))
  }

  // Deletes a single item
  deleteItem(key: string): void {
      this.items.remove(key)
        .catch(error => this.handleError(error))
  }

  // Deletes the entire list of items
  deleteAll(): void {
      this.items.remove()
        .catch(error => this.handleError(error))
  }


  // Default error handling for all actions
  private handleError(error) {
    console.log(error)
  }


}

I tried to remove from the declaration null, then generates another error as undefined.

2 answers

4

The array items is being initialized with null, so an exception is generated when .push() is called. It is necessary to initialize items with a array empty, so items will be defined:

items: FirebaseListObservable<Project[]> = [];
  • I did that, now it generated this: Type 'Undefined[]' is not Assignable to type 'Firebaselistobservable<Project[]>'. Property '$ref' is Missing in type 'Undefined[]'.

2

I managed to solve, added in the service constructor the initialization of the list this.items = db.list('/projects');

constructor(private db: AngularFireDatabase) {
        this.items = db.list('/projects');
   }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.