认证后无法将用户输入发送到firebase
问题描述:
我想在用户身份验证后将用户输入发送到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>
答
有没有在你的代码中的错误。您正在尝试使用与``一起使用的模板文字,并允许您使用字符串参数,但您使用的是常见字符串字符('')。
为了解决你的代码,你应该改变你的代码:
this.db.list('profile/${auth.uid}').push(this.uname)
到:
this.db.list(`profile/${auth.uid}`).push(this.uname)
进一步提供文件关于模板文字看看MDN Template Literals
感谢我想这个错误是固定的,但我现在有另一个:/错误:Reference.push f ailed:第一个参数在属性中包含一个函数 – rektandlove
@rektandlove因为我无法访问您的项目,所以我无法调试它,它确实很难。该错误说'this.uname'具有作为对象字段的功能。这是@ViewChild装饰器正在做的事情。创建一个对象,其中包含要在集合中保存的属性: '... push({name:this.uname.name,otherField:this.uname.otherField})...' –
谢谢我解决了使用ngmodel在html – rektandlove