我正在尝试向Firebase和ionic注册, 但我从一开始就面临问题,
错误错误:Reference.child失败:第一个参数是无效路径=“未定义”。路径必须是非空字符串,并且不能包含“。”,“#”,“ $”,“ [”或“]”,
有人可以帮我吗
Vector has 2 items and 3 capacity
Value at 0 is 0x123456
Value at 1 is 0x654321
Press any key to continue . . .
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
});
}
}
预先感谢
答案 0 :(得分: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
);
});