0
我正在轉圈,希望有人能指引我正確的方向。AngularFire2:訪問用戶配置文件
業務問題 我有一個AngularFire2應用程序,我要執行以下步驟:
- 用戶登記作爲火力地堡
- 用戶的用戶然後填寫與附加信息的登記表(客戶或組織)。然後,他們點擊保存
- 新客戶創建
- 用戶配置文件與步驟創建客戶的客戶的關鍵在3
- 用戶被重定向到客戶列表頁和它只顯示任何客戶與客戶ID在用戶登錄簡介
我的所有步驟的工作,但我似乎無法找出是如何觸發調用之前來獲取用戶的個人資料信息在客戶服務中獲取所有客戶即
理想情況下,我想要做的是將用戶服務注入客戶服務,以確定當前用戶是誰以及他們的客戶ID是什麼,所以我可以將其作爲參數傳遞給獲取所有客戶功能。
我可以輕鬆調用AuthService並訂閱firebase authstate上的uid,但我無法弄清楚如何從那裏查找用戶配置文件。
代碼
AuthService
import { Injectable } from '@angular/core';
import { AngularFire, AuthProviders, AuthMethods, FirebaseAuthState, FirebaseListObservable } from 'angularfire2';
import { Observable } from 'rxjs/Rx';
import { Router } from '@angular/router';
import { UserModel } from '../../user/models/user.model';
@Injectable()
export class AuthService {
user: UserModel;
userProfile: any;
userId: string;
usersRef: string = '/users/';
constructor(private af: AngularFire, private router: Router) {
this.af.auth.subscribe(authState => {
this.user = authState;
console.log('authService Constructor: ', this.user)
});
}
register(email?: string, password?: string) {
this.af.auth.createUser({ email, password })
.then(response => this.router.navigate(['/user/add/']))
.catch(error => console.log(error));
}
isAuth(): UserModel {
return this.user;
}
}
用戶服務
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
import { AuthService } from '../../auth/services/auth.service';
import { FirebaseService } from '../../core/services/firebase.service';
import { CustomerService } from '../../customer/services/customer.service';
import { UserModel } from '../models/user.model';
import { CustomerModel } from '../../customer/models/customer.model';
@Injectable()
export class UserService {
usersRef: string = '/users/';
user: UserModel;
customer: CustomerModel = new CustomerModel(null, null, null, null, null, null, null, null);
customerKey: string;
userId: string;
constructor(
private af: AngularFire,
private authService: AuthService,
private customerService: CustomerService,
private firebaseService: FirebaseService,
private router: Router) {
}
getAllUsers(): FirebaseListObservable<UserModel[]> {
return this.af.database.list(this.usersRef);
}
getUser(){
return this.af.database.list(this.usersRef, {
query: {
orderByKey: true,
equalTo: this.userId
}
})
}
addUser(user: any) {
this.createNewCustomer(user.organisation);
this.createUser(user);
this.saveUser();
}
createUser(user: UserModel) {
this.user = user;
this.user.createdDate = user.createdDate = Date.now();
this.user.customerId = this.customerKey;
}
saveUser() {
this.af.database.list(this.usersRef).push(this.user)
.then(response => {
this.redirectToCustomerList();
});
}
createNewCustomer(organisation: string) {
this.customerKey = this.firebaseService.createUniqueKey('customers');
this.customer.organisation = organisation;
this.customerService.addCustomer(this.customer, this.customerKey);
}
redirectToCustomerList() {
this.router.navigate(['/customer/list']);
}
}
客服
import { Injectable } from '@angular/core';
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
import { CustomerModel } from '../models/customer.model';
@Injectable()
export class CustomerService {
customersRef: string = '/customers/';
customer: CustomerModel;
userId: string;
constructor(private af: AngularFire) {
}
getAllCustomers(): FirebaseListObservable<CustomerModel[]> {
return this.af.database.list(this.customersRef, {
query: {
orderByKey: true,
equalTo: this.userId
}
})
}
getCustomer(id: string): FirebaseObjectObservable<any> {
return this.af.database.object(this.customersRef + id);
}
addCustomer(customer: CustomerModel, customerKey?: string): any {
customer.createdDate = Date.now();
if (customerKey) {
this.af.database.object(this.customersRef + customerKey).set(customer);
} else {
this.af.database.list(this.customersRef).push(customer);
}
return customerKey;
}
updateCustomer(customerIndex: string, customer: CustomerModel) {
this.af.database.object(this.customersRef).update(customer);
}
}
我剛剛得到一個錯誤說switchMap不是函數類型錯誤:this.af.auth.map(...)switchMap不是一個函數 – ccocker
你可能忘了將其導入 - 導入'「rxjs/add/operator/switchMap'' – Sasxa
正確:-)。我不熟悉開關圖,那麼我如何從中取出實際的customerKey。我得到一個匿名主題 – ccocker