0

I want to use RxJs in my LWC. I was successfully able to import RxJs version 5. However, how can I use it with LoadScript. This is my code, it is giving me the following error "of is not a function at eval":


loadScript(this, rxjs +'/rxjs').then(()=>{
  of(1,2,3).pipe(map(x => x + '!!!')))
}).catch((error)=>{
     /* eslint-disable no-undef */ 
   console.log('Files not.: ', rxjs, error);
})

Can someone help with that?

2
  • Please post all the relevant code. RxJS as a static resource should work. Sep 29, 2019 at 6:12
  • @KevinVenkiteswaran I was able to import RxJs version 5 only. However, the code up there is still not working. I am getting the following error "of is not a function at eval" Oct 1, 2019 at 15:10

1 Answer 1

0
import { LightningElement } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import executeQuery from '@salesforce/apex/AccountsWithContact_cls.executeQuery'; // @AuraEnabled method

import reactivexUrl from '@salesforce/resourceUrl/ReactiveX'; // Static resource of https://unpkg.com/rxjs/bundles/rxjs.umd.min.js

export default class AccountsWithContacts extends LightningElement {

    renderedCallback() {
        Promise.all([
            loadScript(this, reactivexUrl)
        ]).then(() => {
            this.loadAccountsWithContacts();
            this.init
        }).catch(error => { })
    }

    /**
     * Returns an Observable that makes a call to a Salesforce method
     * @param {string} query soql query to execute in Salesforce
     */
    getData$( objParams = {} ) {
        return rxjs.defer( async () => {
            const response = await executeQuery(objParams);
            return response;
        }) ;
    }
    /**
     * Stream 1
     * List of Account
     */
    getAccountList$() {
        let query = 'SELECT Id, Name FROM Account ORDER BY CreatedDate DESC LIMIT 2';
        return this.getData$({ query });
    }

    loadAccountsWithContacts() {
        this.getAccountList$()
        .pipe(
            rxjs.operators.flatMap(( x ) => x)
        )
        .subscribe(( accounts ) => { // my subscription
            console.log('Accounts: ', { accounts });
        })
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .