我有一个getUserInfo函数,可以成功返回用户的ID,电子邮件等。 我也有一个updateUserEmail函数,该函数使用flatMap来合并GET请求(getUserInfo())以进行服务器验证,然后进行PUT请求。我以前从未使用过flatMap,所以我也在尝试找出在哪里进行验证。我不确定是否要对get UserInfo函数进行验证,但这似乎是最合逻辑的地方。我需要在PUT请求之前先验证GET请求,以防验证失败并且我希望PUT请求不会发生。
我也知道我没有使用flatMap的userInfo值。这是因为我不太确定该怎么做。这不是服务器响应,我可以在其中获取userInfo._id。我对这一切都很陌生,因此感谢您的帮助。
用户服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { UserEmailChange } from './emailChange.model';
import { flatMap } from 'rxjs/operators';
import { AuthService } from '../auth.service';
import { tap } from 'rxjs/operators';
@Injectable({ providedIn: 'root' })
export class ProfileService {
userId = localStorage.getItem("userId: ");
constructor(private http: HttpClient, private authService: AuthService) { }
getUserInfo(id: string, oldEmail: string) {
this.http.get(`http://localhost:3000/api/user/${id}`).pipe(tap(value => 'output: ' + "TEST" + value)).subscribe((res) => {
if (this.userId === res["posts"]._id && oldEmail === res["posts"].email) {
console.log("You passed the id and email test");
}
else {
console.log("You failed the test!");
}
});
}
updateUserEmail(emailChange: UserEmailChange) {
return this.getUserInfo(this.userId, emailChange.oldEmail)
.pipe(flatMap(userInfo => this.http.put(`http://localhost:3000/api/user/${this.userId}`, emailChange )));
}
}
用户组件
import { Component, OnInit } from '@angular/core';
import { ProfileService } from './profile.service';
import { AuthService } from '../auth.service';
import { UserEmailChange } from './emailChange.model';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
userId: string;
authenticated = false;
emailResponse: string;
idResponse: string;
oldEmail: string;
constructor(private profileService: ProfileService, private authService: AuthService) { }
ngOnInit() {
this.userId = localStorage.getItem("userId: ");
}
onUpdateUserEmail(oldEmail: string, newEmail: string) {
const userEmailChange = new UserEmailChange();
userEmailChange.oldEmail = oldEmail;
userEmailChange.newEmail = newEmail;
this.profileService.updateUserEmail(userEmailChange).subscribe(emailUpdated => {
});
}
}
当前在服务的updateUserEmail()中,.pipe返回错误
Property 'pipe' does not exist on type 'void'.ts
答案 0 :(得分:0)
正如@Alexander所述,您没有从getUserInfo
返回正确的值。您需要返回可观察的对象,然后在将其返回给您的函数中进行操作
getUserInfo(id: string, oldEmail: string) {
return this.http.get(`http://localhost:3000/api/user/${id}`)
.pipe(
tap(value => 'output: ' + "TEST" + value),
tap(res => {
if (this.userId === res["posts"]._id && oldEmail === res["posts"].email)
console.log("You passed the id and email test");
else console.log("You failed the test!");
})
);
}
updateUserEmail(emailChange: UserEmailChange) {
return this.getUserInfo(this.userId, emailChange.oldEmail)
.pipe(
flatMap(userInfo => this.http.put(`http://localhost:3000/api/user/${this.userId}`, emailChange))
);
}