Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法使用bindNodeCallback_Angular_Typescript_Rxjs_Auth0 - Fatal编程技术网

Angular 无法使用bindNodeCallback

Angular 无法使用bindNodeCallback,angular,typescript,rxjs,auth0,Angular,Typescript,Rxjs,Auth0,我正在尝试这个: 返回Observable.bindNodeCallback(this.webAuth.client.userInfo)(this.accessToken) 我正在尝试从Auth0 userInfo函数中创建一个可观察的,这样我就可以在AuthGuard的canActivate中使用它 但我得到了一个错误: TS2346:提供的参数与呼叫目标的任何签名都不匹配。 上面的库函数是: Authentication.prototype.userInfo=函数(accessToken,

我正在尝试这个:

返回Observable.bindNodeCallback(this.webAuth.client.userInfo)(this.accessToken)

我正在尝试从Auth0 userInfo函数中创建一个可观察的,这样我就可以在AuthGuard的canActivate中使用它

但我得到了一个错误:

TS2346:提供的参数与呼叫目标的任何签名都不匹配。

上面的库函数是:

Authentication.prototype.userInfo=函数(accessToken,cb){
var-url;
assert.check(accessToken{
键入:“字符串”,
消息:“accessToken参数无效”
});
assert.check(cb{
类型:“函数”,
消息:“cb参数无效”
});
url=urljoin(this.baseOptions.rootUrl,'userinfo');
返回此文件。请求
.get(url)
.set('Authorization'、'Bearer'+accessToken)
.结束(负责人(cb、{
无知者:对
}));

};我遇到了完全相同的问题。最后,我不使用
bindNodeCallback()
,而是喜欢这样(注意:外部
userInfo()
函数是我的
AuthProvider
类的一部分):

我遇到的一些陷阱:

  • 确保在测试中使用
    flatMap()!我对那件事一直耿耿于怀
  • 我是新来的,在我真正了解发生的事情之前,我必须反复学习很多次

您的问题是,原始方法希望访问
this
关键字以访问Auth0客户端的内部属性。当您想要将此客户端的方法包装到可观察的实例时,必须将其绑定到该实例。以下是一个例子:

const userInfo = Observable.bindNodeCallback(this.webAuth.client.userInfo);
userInfo.call(this, this.accessToken);
// or
userInfo.bind(this)(this.accessToken);

毕竟,您可以将响应用作可观察的响应。

您可以通过明确定义参数来实现这一点,如下所示:

const fetchProfile$ = Observable.bindNodeCallback((
  accessToken: string,
  callback: Auth0Callback<Auth0UserProfile>
) => this.auth0.client.userInfo(accessToken, callback));

return fetchProfile$(accessToken);
const fetchProfile$=Observable.bindNodeCallback((
accessToken:string,
回调:Auth0Callback
)=>this.auth0.client.userInfo(accessToken,callback));
返回fetchProfile$(accessToken);

我知道这是一个旧线程,但我已经能够使我的auth0 userInfo方法使用bindNodeCallback

以下是我的解决方案,如果它能帮助某人:

// must use the bind() method to change scope of "this" inside the userInfo function
private _userInfo$ = bindNodeCallback(this._Auth0.client.userInfo.bind(this._Auth0.client));

// Get user information
public getUserInfo$() : Observable<any>{

  return this.token$.pipe(
    switchMap(token => this._userInfo$(token))
  );

}

// Check if users email is verified
public checkEmailVerified$() : Observable<boolean>{

  return this.token$.pipe(
    switchMap(token => this._userInfo$(token)),
    map(userInfo => userInfo.email_verified )
  );

}
//必须使用bind()方法更改userInfo函数中“this”的范围
private _userInfo$=bindNodeCallback(this._Auth0.client.userInfo.bind(this._Auth0.client));
//获取用户信息
公共getUserInfo$():可观察{
返回此.token$.pipe(
switchMap(令牌=>this.\u userInfo$(令牌))
);
}
//检查用户的电子邮件是否已验证
public checkEmailVerified$():可观察{
返回此.token$.pipe(
switchMap(令牌=>this.\u userInfo$(令牌)),
映射(userInfo=>userInfo.email\u已验证)
);
}

您检查过了吗:?当然,我相信我正确地遵循了说明!当然是。你是说acessToken是一个错误对象?哦!!我认为回调函数中的第一个参数必须是一个错误对象:-(很好的提示,它使我在解决问题方面取得了进步。谢谢
const fetchProfile$ = Observable.bindNodeCallback((
  accessToken: string,
  callback: Auth0Callback<Auth0UserProfile>
) => this.auth0.client.userInfo(accessToken, callback));

return fetchProfile$(accessToken);
// must use the bind() method to change scope of "this" inside the userInfo function
private _userInfo$ = bindNodeCallback(this._Auth0.client.userInfo.bind(this._Auth0.client));

// Get user information
public getUserInfo$() : Observable<any>{

  return this.token$.pipe(
    switchMap(token => this._userInfo$(token))
  );

}

// Check if users email is verified
public checkEmailVerified$() : Observable<boolean>{

  return this.token$.pipe(
    switchMap(token => this._userInfo$(token)),
    map(userInfo => userInfo.email_verified )
  );

}