简体   繁体   中英

Observable value inside subscribe is undefined when chaning observables

I have a function from a certain library that returns an Observable that I want to call from another function. I have a need to propagate that Observable into multiple function calls. Here is how my code is structured:

extractSignature = (xml, signatureCount = 1) => {
    const observ = this.generateDigest(signedContent, alg).pipe(map(digest => {
            const sigContainer = {
                alg: alg,
                signature: signatureValue,
                signedContent: signedContent,
                digest: digest
            };
            console.log('sigContainer inside pipe: ');
            console.log(sigContainer);
            return sigContainer;
        }));

        return observ;
}

dissasemble(xml): Observable<SignatureContainerModel[]> {
    const observables: Observable<any>[] = [];
        for (let i = 1; i <= count; i++) {
            const extractSigObservable = this.extractSignature(xml, i);

            console.log('extractSigObs inside pipe: ');
            console.log(extractSigObservable);

            const observ = extractSigObservable.pipe(map(sigContainer => {
                console.log('sigContainer inside pipe: ');
                console.log(sigContainer);

                const hashContainers: HashContainerModel[] = [];
                const hashContainer: HashContainerModel = new HashContainerModel();
                hashContainer.digestAlgorithm = sigContainer.alg;
                hashContainer.bytes = sigContainer.digest;
                hashContainers.push(hashContainer);
                const signatureContainer: SignatureContainerModel = {
                    hashContainers: hashContainers,
                    signature: sigContainer.signature
                };

                console.log('observable inside pipe: ');
                console.log(observ);
            }));


            observables.push(observ);
        }

        return forkJoin(observables);
}

verify() {
    this.sigExec.dissasemble(this.fileContent).subscribe((signatureContainers: SignatureContainerModel[]) => {
                // signatureContainers is [undefined] here
                console.log('Sig Containers: ');
                console.log(signatureContainers);
                this.verifyHash(signatureContainers);
            });
}

signatureContainers variable is [undefined] inside the subscribe. I'm not sure what the problem is since when I check all the logs that I wrote inside map functions they seem fine

RXJS Documentation on forkJoin :

Be aware that if any of the inner observables supplied to forkJoin error you will lose the value of any other observables that would or have already completed if you do not catch the error correctly on the inner observable. If you are only concerned with all inner observables completing successfully you can catch the error on the outside. https://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin

There a possibility you are erroring out inside your pipe and those values are being lost.

Also, I noticed that you're not returning anything from your pipe. This could also be an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM