【问题标题】:Unable to send user input to firebase after authentication身份验证后无法将用户输入发送到 Firebase
【发布时间】:2017-10-09 16:11:34
【问题描述】:

我正在尝试在用户身份验证后将用户输入发送到 firebase。我正在使用 Ionicframework。这是我尝试过的。

错误未捕获(承诺中):错误: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>

【问题讨论】:

    标签: typescript firebase ionic-framework


    【解决方案1】:

    您的代码中有错误。您正在尝试使用与 `` 一起使用的模板文字,并允许您使用字符串参数,但您使用的是通用字符串字符 ('')。

    要解决您的代码,您应该更改代码:

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

    到:

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

    有关模板文字的更多文档,请查看MDN Template Literals

    【讨论】:

    • 谢谢,我猜这个错误已修复,但我现在有另一个错误:/错误:Reference.push 失败:第一个参数包含属性中的函数
    • @rektandlove 因为我无法访问您的项目,所以我无法调试它,这真的很难。该错误表明this.uname 具有作为对象字段的功能。这是@ViewChild 装饰器肯定会做的事情。使用您要保存在集合中的属性创建一个对象:...push({ name: this.uname.name, otherField: this.uname.otherField })...
    • @rektandlove 不要忘记将答案标记为正确答案,如果您在代码中发现更多错误,请在问题中澄清您的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 2020-03-12
    • 1970-01-01
    • 2018-07-10
    • 2019-08-31
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多