this.db.object(...).push is not a function in Ionic

时间:2019-03-17 22:22:44

标签: angular firebase ionic-framework

This is my .ts file and i don't know why i am getting the error above. The push on the addClick method has a red dotted line beneath it.

I followed a tutorial and it is the same as the guy did it on his tutorial.

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');
}


}

This is my.html file

<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>

1 个答案:

答案 0 :(得分: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()方法。


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

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

在此处阅读文档:

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