我知道如何使用电子邮件和密码创建用户,我尝试使用其他属性(用户名,性别,DOB)创建用户。
我在控制台中收到如下错误。
Failed to login!
register.component.ts:62 TypeError: Cannot read property
'createUserWithEmailAndPassword' of undefined
at Observable._subscribe (auth.service.ts:95)
at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:42)
at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:28)
at RegisterComponent.push../src/app/register/register.component.ts.RegisterComponent.singUp (register.component.ts:58)
at Object.eval [as handleEvent] (RegisterComponent.html:5)
at handleEvent (core.js:10258)
at callWithDebugContext (core.js:11351)
at Object.debugHandleEvent [as handleEvent] (core.js:11054)
at dispatchEvent (core.js:7717)
at core.js:8161
在我写Service.ts文件时,下面有我的代码。请帮助我找到错误
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { AngularFireAuth } from 'angularfire2/auth';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { environment } from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class AuthService {
authService: any;
angularFireAuth: any;
af: any;
constructor(private firebaseAuth: AngularFireAuth,
private http:HttpClient,
private afAuth:AngularFireAuth) {
this.user = firebaseAuth.authState;
this.af = firebaseAuth.authState;
}
singUp(email: string, username: string, password: string, compassword:string, gender: string, bday: Date, stripeID:string): Observable<any>{
return new Observable( obs => {
debugger
this.af.auth.createUserWithEmailAndPassword({
// Create user
email: email,
password: password
}).then((user) => {
// User created now create the database user
debugger
return this.af.database.object(`/users/${user.uid}`).update({
name: username,
gender: gender
});
}).then((success) => {
debugger
// Success
console.log(success);
}).catch((error) => {
debugger
// Error
console.log(error);
});
})
}
}
请帮助我解决此错误
答案 0 :(得分:1)
我不知道这是否是Cannot read property 'createUserWithEmailAndPassword' of undefined
错误的原因(我不知道角度),但是这样做时您应该得到一个错误:
.then((user) => {
// User created now create the database user
debugger
return this.af.database.object(`/users/${user.uid}`).update({
name: username,
gender: gender
});
})
事实上,createUserWithEmailAndPassword()
方法返回UserCredential
,如doc中所述。反过来,UserCredential
的{{3}}解释说,它包含一个User
对象。
所以您应该这样做:
.then((userCredential) => {
// User created now create the database user
debugger
return this.af.database.object(`/users/${userCredential.user.uid}`).update({
name: username,
gender: gender
});
})
答案 1 :(得分:1)
从构造函数zip
中删除实例化
并在注册方法中使用var array = ["1-2", "50-100", "0.125-0.25", "100-200"]
array.append("1000-2000")
let firstDoubleArray = array.map{Double($0.components(separatedBy: "-").first!)!}
let sortedArray = zip(array, firstDoubleArray).sorted {$0.1 < $1.1}.map{$0.0}
print(sortedArray)
。这需要两个参数而不是一个对象:
this.af = firebaseAuth.authState;
也请参考雷诺关于this.firebaseAuth.auth.createUserWithEmailAndPassword()
返回类型的答案。因此,根据您的情况,uid位于singUp(email: string, username: string, password: string, gender: string): Observable<any> {
return new Observable(obs => {
this.firebaseAuth.auth.createUserWithEmailAndPassword(
// Create user
email,
password
).then((user) => {
// User created now create the database user
return this.af.database.object(`/users/${user.user.uid}`).update({
name: username,
gender: gender
});
}).then((success) => {
// Success
console.log(success);
}).catch((error) => {
// Error
console.log(error);
});
});
}
为什么还要在构造函数中注入createUserWithEmailAndPassword()
2次?