【问题标题】:Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]" [duplicate]路径必须是非空字符串,并且不能包含“.”、“#”、“$”、“[”或“]”[重复]
【发布时间】:2018-11-24 20:45:15
【问题描述】:

我正在尝试向 firebase 和 ionic 注册, 但我从一开始就面临这个问题,

错误错误:Reference.child 失败:第一个参数是无效路径 =“未定义”。路径必须是非空字符串,并且不能包含“.”、“#”、“$”、“[”或“]”,

谁能帮我解决这个问题

this is the provider:

import * as firebase from 'firebase';
import { Injectable } from '@angular/core';

/*
  Generated class for the UserServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class UserServiceProvider {
  public data: any;
  public fireAuth: any;
  public userProfile: any;

  constructor() {
    console.log('Hello UserServiceProvider Provider');

    this.fireAuth = firebase.auth();

  	this.userProfile = firebase.database().ref(`email`);
  }

  signupUserService(account: {}){

    
    return this.fireAuth.createUserWithEmailAndPassword(account[`email`], account[`password`]).then((newUser) => {
      //sign in the user
      this.fireAuth.signInWithEmailAndPassword(account[`email`], account[`password`]).then((authenticatedUser) => {
        //successful login, create user profile
      this.userProfile.child(authenticatedUser.uid).set(
        account
      );
      });
    });

}

}
------------------------------------------------------

this is my signUp page

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController, ToastController } from 'ionic-angular';
import { UserServiceProvider } from '../../providers/user-service/user-service';
import { HomePage } from '../home/home';

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

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  public skills : string;
  public email : string;
  public phone : any;
  public password : any;
  public first_name : any;
  public last_name : any;
  public city : any;
  public state : any;
  public country : any;
  public isJobSeeker : boolean;

  constructor(public navCtrl: NavController,
     public navParams: NavParams,
     public usersserviceProvider : UserServiceProvider, 
     public toastCtrl: ToastController, public loadingCtrl: LoadingController) {
  }

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

  
  doSignup(){

    var   account = {
      first_name: this.first_name,
      last_name: this.last_name || '',
      skills: this.skills || '',
      email: this.email,
      phone: this.phone || '',
      password: this.password,
      city: this.city || '',
      state: this.state || '',
      country: this.country || '',
      isJobSeeker : this.country || ''

    };
var that = this;

var loader = this.loadingCtrl.create({
      content: "Please wait...",
      
    });
    loader.present();


  	this.usersserviceProvider.signupUserService(account).then(authData => {
  		//successful
  		loader.dismiss();
  		that.navCtrl.setRoot(HomePage);

  	}, error => {
      loader.dismiss();
     // Unable to log in
      let toast = this.toastCtrl.create({
        message: error,
        duration: 3000,
        position: 'top'
      });
      toast.present();

      that.password = ""//empty the password field

  	});

    
  }

}
this my html 

 <ion-list>
        
          <ion-item >
            <ion-label stacked>Skill Set (separate with comma)</ion-label>
            <ion-input type="text" [(ngModel)]="skills" name="skills" placeholder="eg. PHP, Writing, Chef" required="required"></ion-input>
          </ion-item>
          
         <ion-item>
            <ion-label stacked>Email</ion-label>
            <ion-input type="email" [(ngModel)]="email" name="email" placeholder="eg. john@doe.com"></ion-input>
          </ion-item>
          <ion-item >
              <ion-label stacked>Phone</ion-label>
              <ion-input type="text" [(ngModel)]="phone" name="phone" placeholder="eg. 0802222222" required="required"></ion-input>
            </ion-item>
      
      
    
          <ion-item>
            <ion-label stacked>Password</ion-label>
            <ion-input type="password" [(ngModel)]="password" name="password"></ion-input>
          </ion-item>

    <hr/>
          <ion-item>
            <ion-label stacked>First name</ion-label>
            <ion-input type="text" [(ngModel)]="first_name" name="first_name"></ion-input>
          </ion-item>
    
          <ion-item>
            <ion-label stacked>Last name</ion-label>
            <ion-input type="text" [(ngModel)]="last_name" name="last_name"></ion-input>
          </ion-item>
    
          <ion-item>
            <ion-label stacked>City</ion-label>
            <ion-input type="text" [(ngModel)]="city" name="city"></ion-input>
          </ion-item>
          <ion-item>
            <ion-label stacked>State/Province</ion-label>
            <ion-input type="text" [(ngModel)]="state" name="state"></ion-input>
          </ion-item>
    
    
        
    
           <ion-item>
            <ion-label>Looking for Job?</ion-label>
            <ion-toggle [(ngModel)]="isJobSeeker" name="phone" checked="false"></ion-toggle>
          </ion-item>
    
          <div padding text-center>
            <button ion-button color="danger" round (click)="doSignup()" >Signup</button>
          </div>
    
        </ion-list>

提前致谢,

【问题讨论】:

  • 你传递了什么作为authenticatedUser.uid?
  • 什么意思
  • 设置一个console.log(authenticatedUser.uid);
  • 告诉我里面有什么。

标签: firebase ionic3 firebase-authentication


【解决方案1】:

如果您查看signInWithEmailAndPassword 的文档,您会看到它返回一个UserCredential。检查文档表明它没有uid 属性,这解释了为什么你会得到undefined

你会想使用authenticatedUser.user.uid,所以:

this.fireAuth.signInWithEmailAndPassword(account[`email`], account[`password`]).then((userCredential) => {
  this.userProfile.child(userCredential.user.uid).set(
    account
  );
});

使用createUserWithEmailAndPassword 创建一个新用户帐户会自动让他们登录,因此不需要嵌套这些调用。如果您(仅)想在创建帐户时存储用户配置文件,createUserWithEmailAndPassword 也会返回 UserCredential。所以在那里,你也需要间接到user.uid

return this.fireAuth.createUserWithEmailAndPassword(account[`email`], account[`password`]).then((userCredential) => {
  return this.userProfile.child(userCredential.user.uid).set(
    account
  );
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多