http请求传递firebase令牌时出现订阅错误
尝试将firebase令牌传递给nodejs api并检索数据。我收到订阅错误:TSS2339:类型“void”上不存在属性“subscribe”。 用户配置文件数据存储在mongoDB集合中,可通过api访问该集合 这段代码在我的用户服务中起作用http请求传递firebase令牌时出现订阅错误,firebase,firebase-authentication,observable,subscribe,Firebase,Firebase Authentication,Observable,Subscribe,尝试将firebase令牌传递给nodejs api并检索数据。我收到订阅错误:TSS2339:类型“void”上不存在属性“subscribe”。 用户配置文件数据存储在mongoDB集合中,可通过api访问该集合 这段代码在我的用户服务中起作用 public getCurrUser() { const baseUrl = 'http://localhost:3000' return this.http.get(baseUrl + '/userinfo').pipe(map(
public getCurrUser() {
const baseUrl = 'http://localhost:3000'
return this.http.get(baseUrl + '/userinfo').pipe(map((res) => {
console.log(res);
const userinfo = res;
console.log('user docs: ', userinfo);
return userinfo;
}));
}
以下是服务中的代码,用传递用户令牌包装:
getUserData() {
const baseUrl = 'http://localhost:3000'
firebase.auth().currentUser.getIdToken()
.then(authToken => {
const headers = new HttpHeaders({'Authorization': authToken });
return this.http.get(baseUrl + '/userinfo', {headers}).pipe(map((res) => {
console.log('the results of getUserData : ', res);
const userinfo = res;
console.log('logged in user info: ', userinfo);
return userinfo;
}));
});
}
这是未使用subscribe错误编译的代码,该错误正在从组件页调用服务:
public getCurrUserData() {
this.userCurr = null;
console.log('in user admin component - retrieval of curr user')
this.userAdminService.getUserData()
.subscribe(
(userCurr: UserProfileModel[]) => {
this.userCurr = userCurr;
console.log('loading current user: ', this.userCurr);
}
);
}
它似乎与返回一个可观察的对象有关,但我无法确定它的位置。任何hep都会很感激getUserData没有立即返回您可以订阅的可观察数据,因此会出现错误。它首先有一个承诺,解析后返回一个observable,但this.userAdminService.getUserData.subscribe需要getUserData返回一个observable
您可以尝试将观测值转换为实时更改,或者您可以这样做:
getUserData(authToken) {
const baseUrl = 'http://localhost:3000'
const headers = new HttpHeaders({'Authorization': authToken });
return this.http.get(baseUrl + '/userinfo', {headers}).pipe(map((res) => {
console.log('the results of getUserData : ', res);
const userinfo = res;
console.log('logged in user info: ', userinfo);
return userinfo;
}));
}
public getCurrUserData() {
this.userCurr = null;
console.log('in user admin component - retrieval of curr user');
firebase.auth().currentUser.getIdToken()
.then(authToken => {
this.userAdminService.getUserData(authToken )
.subscribe((userCurr: UserProfileModel[]) => {
this.userCurr = userCurr;
console.log('loading current user: ', this.userCurr);
}
);
});
}
您的userAdminService.getUserData属于哪种类型?当您使用vscode时,您可以通过在其上移动鼠标进行检查。看起来您只是忘记了向getUserData函数添加return语句