2017-10-09 81 views
0

我想在用戶身份驗證後將用戶輸入發送到firebase。我正在使用Ionicframework。這是我試過的。認證後無法將用戶輸入發送到firebase

錯誤未捕獲(承諾):錯誤:Reference.child失敗:第一個參數是無效的路徑='profile/$ {auth.uid}'。

打字稿文件:

import { Component,ViewChild } from '@angular/core'; 
import { IonicPage, NavController, NavParams } from 'ionic-angular'; 
import {AngularFireAuth} from 'angularfire2/auth'; 
import {AngularFireDatabase} from 'angularfire2/database'; 
import { TabsPage } from '../tabs/tabs'; 

/** 
* Generated class for the FpPage page. 
* 
* See https://ionicframework.com/docs/components/#navigation for more info on 
* Ionic pages and navigation. 
*/ 

@IonicPage() 
@Component({ 
    selector: 'page-fp', 
    templateUrl: 'fp.html', 
}) 
export class FpPage { 

    @ViewChild('uname') uname; 

    constructor(private fire:AngularFireAuth,private db :AngularFireDatabase,public navCtrl: NavController, public navParams: NavParams) { 
    } 

    createProfile(){ 

this.fire.authState.take(1).subscribe(auth => { 

this.db.list('profile/${auth.uid}').push(this.uname) 
.then(() => this.navCtrl.push('TabsPage')) 


}) 


    } 

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

} 

HTML:

<ion-content padding> 

<ion-item> 
    <ion-label floating>Username</ion-label> 
    <ion-input type="username" #uname></ion-input> 
</ion-item> 



<button ion-button clear block (click)="createProfile()">Set Username</button> 

</ion-content> 

回答

0

有沒有在你的代碼中的錯誤。您正在嘗試使用與``一起使用的模板文字,並允許您使用字符串參數,但您使用的是常見字符串字符('')。

爲了解決你的代碼,你應該改變你的代碼:

this.db.list('profile/${auth.uid}').push(this.uname)

到:

this.db.list(`profile/${auth.uid}`).push(this.uname)

進一步提供文件關於模板文字看看MDN Template Literals

+0

感謝我想這個錯誤是固定的,但我現在有另一個:/錯誤:Reference.push f ailed:第一個參數在屬性中包含一個函數 – rektandlove

+0

@rektandlove因爲我無法訪問您的項目,所以我無法調試它,它確實很難。該錯誤說'this.uname'具有作爲對象字段的功能。這是@ViewChild裝飾器正在做的事情。創建一個對象,其中包含要在集合中保存的屬性: '... push({name:this.uname.name,otherField:this.uname.otherField})...' –

+0

謝謝我解決了使用ngmodel在html – rektandlove

相關問題