unable to convert an Observable into a ConnectableObservable - rxjs

I have a BehaviorSubject. I am trying to convert its Observable into a ConnectableObservable but I am getting error publishBehaviour is not a function. What am I doing wrong?
this.questionsArray$.publishBehavior is not a function TypeError:
this.questionsArray$.publishBehavior is not a function
The code snippet is
private questionsArraySubject: BehaviorSubject<Result>;
questionsArray$: Observable<Result>;
questionsArrayConnectable$:ConnectableObservable<Result>;
this.questionsArraySubject = new BehaviorSubject<Result>(new Result('initial',{}));
// create Observable. Other components can subcribe to it now to get notifications/values
this.questionsArray$ = this.questionsArraySubject.asObservable();
this.questionsArrayConnectable$ = this.questionsArray$.publishBehavior(new Result('initial',{}))

Related

Rxjs map method return undefined

I'm tring to learn rxjs but i'm having some problems with the map operator. Here's my code:
const courses$ = http$.pipe(map(res => {res['payload']}))
courses$.subscribe(
(datas) => {console.log(datas)},
noop,
() => {console.log('finished')}
)
createObservable() is a function that i defined. It simply makes an http request to a db and return the datas in json format. I tested it out and works.
Debbuging I saw that console logging res['payload'] directly in map works, but when i console log it in the observable subscribe return undefined. Here there is a picture of the structure of the dats that i recive from the db. Thank u all
you're not actually returning in your map, change to:
const courses$ = http$.pipe(map(res => res['payload']))
adding {} around the function requires a return statement

Cannot read property 'map' of undefined in rxjs

I am new to Angular 6.X and rxjs. I am trying to implement observable for the click operation. its pulling data through RestApi, but getting below exception at data['list'].map, I have tried different approaches, but none of them worked out.
get(): Observable<ProgramOwnerVulnerability[]> {
//explicitly do not type this get, due to list structure returned
return this.http
.get(this.programListUrl)
.pipe(map((data) => {
//map data appropriately into the correct type
return data['list'].map((programOwnerVulnerabilityJson) => {
return new ProgramOwnerVulnerability(programOwnerVulnerabilityJson);
});
}));
}
Exception:
core.js:1598 ERROR TypeError: Cannot read property 'map' of undefined
at MapSubscriber.project (program-owner.service.ts:29)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:75)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:81)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:85)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)
at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber.notifyNext (mergeMap.js:136)
at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/InnerSubscriber.js.InnerSubscriber._next (InnerSubscriber.js:20)
at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)

Rxjs create observable based on another

Here is the code:
getActiveUserShares(): Observable<UserTeamShare[]> {
return this.userService
.getActiveUser()
.pipe(flatMap((user) => (user ? this.provider.getUserShares(user.id) : of([]))));
}
It throws :
ERROR TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
No idea why.
How do I create observable that when first thing emits it returns and observable thatis based on the first observable?
If this.provider.getUserShares(user.id) returns an Observable<UserTeamShare[]> you should have UserTeamShare[] as the type of the variable defined within the subscription method.
For instance, let's consider the following code
getActiveUserShares() {
return this.userService
.getActiveUser()
.pipe(flatMap((user) => (user ? this.provider.getUserShares(user.id) : of([]))));
}
getActiveUserShares().submit(data => // do stuff with data)
Using intellisense you should see that Typescript tells you that the method getActiveUserShares return an Observable<UserTeamShare[]> while the variable data is of type UserTeamShare[].
If this is the case you should be able to operate with dataas you would with any array of UserTeamShare instances. It this is not the case, than this mean that this.provider.getUserShares(user.id) does not returns an Observable<UserTeamShare[]>.

RxJS: How to delay execution of the ELSE in RxJS.Observable.if?

I have an object that might or might not exist. I'm using Observable.if to determine what action to do based on its existence.
However, the else part of Observable.if seems to be running even when the object is undefined. I get this error: TypeError: Cannot read property 'genesisHash' of undefined
console.log("jjjjjjjjjjjjjjjj: ", t); // prints out jjjjjjjjjjjjjjjj: undefined
return Observable.if(
() => !t,
Observable.of(nodeActions.requestGenesisHashes()),
Observable.of(
nodeActions.treasureHunt({
genesisHash: t.genesisHash // error occurs here
})
)
);
How would I delay the call to nodeActions.treasureHunt so that it doesn't try looking at the genesisHash attribute on t?
I'm using this through redux-observable by the way. Hence using actions.
Your then/else observable creation is not wrapped in a function, so the else observable setup code is ran when passing to the Observable.of(...) function. You probably need to just use regular if/else type of logic:
const actions = !t ? nodeActions.requestGenesisHashes() :
nodeActions.treasureHunt({ genesisHash: t.genesisHash });
return Observable.of(actions);
If you really wanted to use that method, you could just create the Observable manually:
return Observable.if(
() => !t,
Observable.of(nodeActions.requestGenesisHashes()),
Observable.create(obs => {
obs.next(nodeActions.treasureHunt({ genesisHash: t.genesisHash }));
obs.complete();
});
);
That should delay the use of the t variable until something tries to subscribe to the observable, which will never happen.

Error using the .next method

For several days I have been trying to use the .next method to add data.
In trying this :
messages: Observable<Message[]>;
const newMessage = new Message(objMessage);
messages.next(newMessage);
I have this error:
Property 'next' does not exist on type 'Observable<Message[]>'.
Does the .next method work well in an Observable stream?
The function next is not a function on the Observable. It's a function on the Observer.
You could use a Subject which acts as an Observer and Observable at the same time. So something like this:
const messages = new Subject<Message[]>();
const newMessage = new Message(objMessage);
messages.next([newMessage]);
As you have an array of Message, you also have to use next with an array.
For more information: http://reactivex.io/documentation/subject.html

Resources