- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试这个:
返回 Observable.bindNodeCallback(this.webAuth.client.userInfo)(this.accessToken);
我正在尝试从 Auth0 userInfo 函数创建一个可观察对象,以便我可以在 AuthGuard 的 canActivate 中使用它。
但是我收到了这个错误:
TS2346:提供的参数与调用目标的任何签名都不匹配。
上面的库函数是:
Authentication.prototype.userInfo = function(accessToken, cb) {
var url;
assert.check(accessToken, {
type: 'string',
message: 'accessToken parameter is not valid'
});
assert.check(cb, {
type: 'function',
message: 'cb parameter is not valid'
});
url = urljoin(this.baseOptions.rootUrl, 'userinfo');
return this.request
.get(url)
.set('Authorization', 'Bearer ' + accessToken)
.end(responseHandler(cb, {
ignoreCasing: true
}));
};
最佳答案
我遇到了完全相同的问题。最终,我没有使用 bindNodeCallback()
,而是 discovered you can just wrap the entire call to userInfo()
in Observable.create()
像这样(注意:外部 userInfo()
函数是我的 AuthProvider
类的一部分):
userInfo = (token: string): Observable<User> => Observable.create(observer => {
this.auth0.client.userInfo(token, (err: any, user: User) => {
if (err) {
observer.error(err);
}
observer.next(user);
observer.complete();
});
});
如果你想对上面的代码进行单元测试,我会使用惊人的 rxjs-marbles
library由 @cartant 开发(谁在上面的评论中做出了回应)。以下是我测试相关部分的一些片段:
import { TestBed } from '@angular/core/testing';
import { marbles } from 'rxjs-marbles/jest'; // <-- add /jest here to level-up!
import { Observable } from 'rxjs';
import { StoreModule, Store } from '@ngrx/store';
import { AuthProvider } from './auth'; // my authentication service
// Here's a mock for the relevant bits of the Auth0 library
jest.mock('auth0-js', () => ({
WebAuth: options => ({
client: {
userInfo: (token, cb) => {
if ( "error" === token ) {
cb(new Error("Profile error!"), null);
} else {
cb(null, {name: "Alice"});
}
}
}
})
}));
describe("Auth0.WebAuth.client", () => {
let auth: AuthProvider;
let store: Store<any>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ StoreModule.forRoot({}) ],
providers: [ AuthProvider ]
});
store = TestBed.get(Store);
spyOn(store, 'pipe');
auth = TestBed.get(AuthProvider);
});
it("should return Observable<User> when calling userInfo()", marbles((m) => {
const user = { name: "Alice" }; // must match mocked value above
const source = m.cold("--a", { a: "dummy-access-token" });
const target = m.cold("--b", { b: user });
const profile = source.flatMap(auth.userInfo);
m.expect(profile).toBeObservable(target);
}));
it("throw Error on fail when calling userInfo()", marbles((m) => {
const err = new Error("Profile error!"); // must match mocked value above
const source = m.cold("--a", { a: "error" }); // this value triggers mock error
const target = m.cold("--#", null, err);
const profile = source.flatMap(auth.userInfo);
m.expect(profile).toBeObservable(target);
}));
});
我遇到的一些陷阱:
flatMap()
!我永远卡在那个上面关于angular - 无法使用 bindNodeCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071996/
我正在尝试这个: 返回 Observable.bindNodeCallback(this.webAuth.client.userInfo)(this.accessToken); 我正在尝试从 Auth
假设我有一个类 import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/bindNodeCallback';
我想知道bindCallback 和bindNodeCallback 方法有什么区别,在RxJs API ;以及如何知道何时使用一种或另一种。 非常感谢! 最佳答案 唯一的区别是 Node 回调总是将
我正在尝试使用 rxjs 5在 TypeScript 中编写 Node.js 服务器,但在将 fs.readFile 转换为其 rxjs 形式时遇到错误。我希望以下代码可以在 TypeScript 中
我是一名优秀的程序员,十分优秀!