【问题标题】:this.db.object(...).push is not a function in Ionicthis.db.object(...).push 不是 Ionic 中的函数
【发布时间】:2019-08-08 07:12:37
【问题描述】:

这是我的 .ts 文件,我不知道为什么会出现上述错误。 addClick 方法的推送在其下方有一条红色虚线。

我遵循了一个教程,这与那个人在他的教程中所做的一样。

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 
'ionic-angular';
import { AngularFireDatabase , AngularFireObject } 
from 'angularfire2/database';
import { Observable } from 'rxjs';



@IonicPage()
@Component({
selector: 'page-create-quiz',
templateUrl: 'create-quiz.html',
})
export class CreateQuizPage {
tasks : Observable <any> ; 
myInput;

constructor(public db:AngularFireDatabase){
this.tasks = this.db.object('/tasks').valueChanges()
this.tasks .subscribe(data => {
  this.myInput = data;
})
}

addclick(){
this.db.object<any>('/tasks').push(this.myInput);
}



ionViewDidLoad() {
console.log('ionViewDidLoad CreateQuizPage');
}


}

这是我的.html 文件

<ion-header>
<ion-navbar>
<ion-title>Create you quiz here</ion-title>
</ion-navbar>
</ion-header>

<ion-content padding>
<ion-item>
<ion-input [(ngModel)] = "myInput" ></ion-input>


</ion-item>
<button (click)= "addclick()"> Add</button>
</ion-content>

【问题讨论】:

    标签: angular firebase ionic-framework firebase-realtime-database


    【解决方案1】:

    object()AngularFireDatabase 类中的一个方法:

     object<T>(pathOrRef: PathReference): AngularFireObject<T>  {
      const ref = getRef(this.database, pathOrRef);
      return createObjectReference<T>(ref, this);
     }
    

    https://github.com/angular/angularfire2/blob/master/src/database/database.ts#L37

    由于它是AngularFireObject 类型,那么您可以使用以下方法:

    method  
    
    set(value: T)       Replaces the current value in the database with the new value specified as the parameter. This is called a destructive update, because it deletes everything currently in place and saves the new value.
    update(value: T)    Updates the current value with in the database with the new value specified as the parameter. This is called a non-destructive update, because it only updates the values specified.
    remove()            Deletes all data present at that location. Same as calling set(null).
    

    https://github.com/angular/angularfire2/blob/master/docs/rtdb/objects.md#api-summary

    因此,没有push() 方法。


    在您的代码中,您似乎只想添加数据,因此您可以使用AngularFireList 中的push()。示例:

    const itemsRef = db.list('items');
    itemsRef.push({ name: newName });
    

    在此处阅读文档:

    https://github.com/angular/angularfire2/blob/master/docs/rtdb/lists.md

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 2019-12-11
      • 2021-05-17
      • 2018-04-09
      • 2020-04-12
      • 2017-07-17
      • 2019-06-05
      • 2021-03-18
      • 1970-01-01
      相关资源
      最近更新 更多