我正在尝试在数据库中创建一个新对象进行评估,现在对其进行硬编码以尝试使其正常运行,但从视图来看将是动态的。我目前收到一个错误,无法读取null的属性“ push”,这使我认为未正确传递回评估对象?还是与Firebase尚无对象等待接收它有关?

我从字面上重复了我在其他地方使用过的代码,这些代码无法确定我在做什么错!

模型

export class AssessmentItem {
  $key: string;
  first: string;
  second: string;
  third: string;
  education: number;
  employment: number;
  health: number;
  social: number;
  civic: number;
  housing: number;
}


服务

  assessmentItems: FirebaseListObservable<AssessmentItem[]> = null;
  assessmentItem: FirebaseObjectObservable<AssessmentItem> = null;

  constructor(private db: AngularFireDatabase,
              private  afAuth: AngularFireAuth) {
    // setting userId returned from auth state to the userId on the service, now we can query
    // currently logged in user, using the id. IMPORTANT
    this.afAuth.authState.subscribe(user => {
      if(user) this.userId = user.uid
    })
  }

createAssessmentItem(assessmentItem: AssessmentItem) {
     this.assessmentItems.push(assessmentItem)
       .catch(error => this.handleError(error))
   }


ts文件

   assessments: FirebaseListObservable<AssessmentItem[]>;

  assessment: AssessmentItem = new AssessmentItem;

  constructor(private visionService: VisionService,
              public auth: AuthService) { }

  ngOnInit() {
    //this.assessments = this.visionService.getAssessment();
  }
 test() {
    this.assessment.first = "test";
    this.assessment.second = "test";
    this.assessment.third = "test";
    this.assessment.education = 1;
    this.assessment.civic = 1;
    this.assessment.employment = 1;
    this.assessment.health = 1;
    this.assessment.housing = 1;
    this.assessment.social = 1;
    this.visionService.createAssessmentItem(this.assessment)
    console.log(this.assessment);
  }


查看如何使用

<md-card-content>
    <p>List 3 things that they did that you think helped them become successful:</p>
    <md-input-container>
      <textarea mdInput
                placeholder="First"
                [(ngModel)]="assessment.first"
                (ngModelChange)="assessment.first"></textarea>
    </md-input-container>
    <md-input-container>
      <textarea mdInput
                placeholder="Second"
                [value]="assessment.second"
                [ngModel]="assessment.second"
                (ngModelChange)="assessment.second"></textarea>
    </md-input-container>
    <md-input-container>
      <textarea mdInput
                placeholder="Third"
                [value]="assessment.third"
                [ngModel]="assessment.third"
                (ngModelChange)="assessment.third">></textarea>
    </md-input-container>
  </md-card-content>
</md-card>


<md-card class="result">
  <md-card-content>
    <p>Education</p>
    <md-slider
      [min]="min"
      [max]="max"
      [step]="step"
      [thumb-label]="thumbLabel"
      [tick-interval]="tickInterval"
      [value]="0"
      [ngModel]="assessment.education"
      (ngModelChange)="assessment.education">
    </md-slider>
    <p>Employment</p>
    <md-slider
      [min]="min"
      [max]="max"
      [step]="step"
      [thumb-label]="thumbLabel"
      [tick-interval]="tickInterval"
      [value]="0"
      [ngModel]="assessment.employment"
      (ngModelChange)="assessment.employment">
    </md-slider>
    <p>Health and Wellbeing</p>
    <md-slider
      [min]="min"
      [max]="max"
      [step]="step"
      [thumb-label]="thumbLabel"
      [tick-interval]="tickInterval"
      [value]="step - 1"
      [ngModel]="assessment.health"
      (ngModelChange)="assessment.health">
    </md-slider>
    <p>Social Connections</p>
    <md-slider
      [min]="min"
      [max]="max"
      [step]="step"
      [thumb-label]="thumbLabel"
      [tick-interval]="tickInterval"
      [value]="step - 1"
      [ngModel]="assessment.social"
      (ngModelChange)="assessment.social">
    </md-slider>
    <p>Civic Participation</p>
    <md-slider
      [min]="min"
      [max]="max"
      [step]="step"
      [thumb-label]="thumbLabel"
      [tick-interval]="tickInterval"
      [value]="step - 1"
      [ngModel]="assessment.civic"
      (ngModelChange)="assessment.civic">
    </md-slider>
    <p>Housing and Living Skills</p>
    <md-slider
      [min]="min"
      [max]="max"
      [step]="step"
      [thumb-label]="thumbLabel"
      [tick-interval]="tickInterval"
      [value]="step - 1"
      [ngModel]="assessment.housing"
      (ngModelChange)="assessment.housing">
    </md-slider>
  </md-card-content>
</md-card>
<button md-raised-button (click)="test()">test button</button>

最佳答案

在您的服务中,您没有为assessmentItemsassessmentItems创建实例,因此它们是null-用来初始化它们的值。

您需要调用注入的listdb方法为assessmentItems创建一个实例:

constructor(
  private db: AngularFireDatabase,
  private afAuth: AngularFireAuth) {

  this.assessmentItems = db.list('some/path/some/key');
}


您还需要为assessmentItem创建一个实例,但是对于代码片段来说,您打算如何(或在何处使用)尚不清楚。

08-07 09:41