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