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)
Related
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[]>.
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.
I have a function 'open' that I want to return a promise to the calling method, so that in my close function I resolve the Promise
open(config){
this.closePromise = new Promise()
return this.closePromise
}
close(closeArgs){
this.closePromise.resolve(closeArgs)
...
}
... so that my calling method gets a callback when my 'close' function is called like this :
myService.open(myData)
.then( closeArgs => console.log('closed'))
I am getting errors like 'Promise resolver undefined is not a function'
I am looking at the docs here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve but nothing seems to fit my needs. ( Im starting to wonder if I should just use an Observable in this scenario, as it may fit my situation better ).
Is this a bad scenario to be using a Promise or am I just doing it wrong ?
Thanks
A promise does not have a resolve method. You need to pass an executor callback to the new Promise constructor to gain access to the resolving function:
open(config) {
return new Promise(resolve => {
if (this.onClose) throw new Error("not yet closed, cannot open (again)");
this.onClose = resolve;
});
}
close(closeArgs) {
if (!this.onClose) throw new Error("not yet opened, cannot close (again)")
this.onClose(closeArgs);
this.onClose = null;
}
I been trying to clear display of all errors onload but those errors should display on further action after load .
I got solution for this but i felt it can been done in a better way
Working solution: This will clear onload messages
ko.utils.arrayForEach(self.MAIN(), function (s) {
ko.utils.arrayForEach(s.STData(), function (s1) {
ko.utils.arrayForEach(s1.SData(), function (s2) {
if (s2.Validation.errors().length > 0) {
s2.Validation.errors.showAllMessages(false);
}
});
});
});
Tried usign group but stuck with error i.e
object doesn't support property or method 'ismodified' in knokcout validation .
Not working :
var result= ko.validation.group(self.MAIN(), { deep: true });
if (!self.MAIN().isValid()) { // rightly i am getting isvalid false and also showing my error text under it .
result.showAllMessages(false); // error at this line as metioned above.
return true;
}
Additional code :
function data(){
var inner = this;
self.name=ko.observable(""); // i have many observables
self.validation = ko.validatedObservable([
self.name.extend({required true, //custom validations also written})
]);
}
But i have this function in observable array which is deeper in self.MAIN
My levels : function data object is pushed into self.SData observable array later this one is pushed into self.STData and finally i pushed this one into 'self.MAIN' .
So you can clearly see i am trying to clear messages onLoad but i get that error . Actually in the mean time i went into validation script file i found i am getting error at this line
ko.utils.arrayForEach(validatables(), function (observable) {
observable.isModified(show); // here isModified is undefined
});
Any suggestions are appreciated .
The Chrome console is giving me this error:
Uncaught TypeError: Cannot call method 'get' of undefined
I search that line and found this:
function _createResponder(element, eventName, handler) {
var registry = Element.retrieve(element, 'prototype_event_registry');
if (Object.isUndefined(registry)) {
CACHE.push(element);
registry = Element.retrieve(element, 'prototype_event_registry', $H());
}
var respondersForEvent = registry.get(eventName);
Uncaught TypeError: Cannot call method 'get' of undefined
if (Object.isUndefined(respondersForEvent)) {
respondersForEvent = [];
registry.set(eventName, respondersForEvent);
}
This seems to be happening when a "null" element is passed to the function on an event binding. Try adding the following before the second line (var registry = ...)
if (element === null) return;
This will basically prevent any further processing within this function, which should not be necessary when the element is null.
I haven't found any issue with doing this on Magento, but please let me know if there is any problem with using this fix.