[英]ERROR TypeError: Cannot read property 'push' of null
I am trying to push to a list and the following error is occurring:我正在尝试推送到列表,但出现以下错误:
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
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)
}
}
ERROR:错误:
ERROR TypeError: Cannot read property 'push' of null
错误类型错误:无法读取 null 的属性“推送”
at在
ProjectService.webpackJsonp.../../../../../src/app/projects/shared/project.service.ts.ProjectService.createItem (project.service.ts:36)ProjectService.webpackJsonp.../../../../../src/app/projects/shared/project.service.ts.ProjectService.createItem (project.service.ts:36)
at在
ProjectFormComponent.webpackJsonp.../../../../../src/app/projects/project-form/project-form.component.ts.ProjectFormComponent.createItem (project-form.component.ts:20)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)在 Object.eval [作为 handleEvent] (ProjectFormComponent.html:8)
at handleEvent (core.es5.js:12047)在 handleEvent (core.es5.js:12047)
at callWithDebugContext (core.es5.js:13508)在 callWithDebugContext (core.es5.js:13508)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13096)在 Object.debugHandleEvent [as handleEvent] (core.es5.js:13096)
at dispatchEvent (core.es5.js:8659)在 dispatchEvent (core.es5.js:8659)
at core.es5.js:9270在 core.es5.js:9270
at HTMLButtonElement.在 HTMLButtonElement。 (platform-browser.es5.js:2668)
(平台浏览器.es5.js:2668)
at在
ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
I have already tried to get the null declaration, it generates another error as undefined.我已经尝试获取空声明,它会生成另一个未定义的错误。
I've found a solution:我找到了一个解决方案:
I started the list in the constructor:我在构造函数中启动了列表:
constructor(private db: AngularFireDatabase) {
this.items = db.list('/projects');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.