简体   繁体   中英

Angular observable.timer not updating url

I have user observable.timer to fetch data after every one minute.Below is the code

getNewData(){
    var catId = this.serv.getSelectedCategory();
          var sinceId = this.myArray[0].userdataId;
            var dataurl = '';
            if (catId < 0) {
                dataurl = this.baseUrl +  '?bookMarked=' + isBookmarked + '&count=' + this.count + '&isPrevious=' + isPrevious + '&sinceId=' + sinceId;

            }
            else {
                dataurl = this.baseUrl + 'company/' + catId + '/abc' + '?bookMarked=' + isBookmarked + '&count=' + this.count + '&isPrevious=' + isPrevious + '&sinceId=' + sinceId;

            }

            var self = this;

            var headers = new Headers();
            headers.append('Content-Type', 'application/x-www-form-urlencoded');
            headers.append('Authorization', token);

            return Observable.timer(60000,60000)
                .switchMap(() => this.http.get(dataurl , { headers: headers, method: 'GET' }))
                .map((res: Response) => {
                    var obj = res.json();
                    return obj;
                });

        }

In the component

this.newDataSubscription = this.testServ.getNewData(false,false).subscribe(data => {
            this.newdataArray = this.serv.newdataArray ;
        })

The issue is the same url is being hit everytime the timer executes.I want to update the sinceId in the url based on the new data that is fetched.

So everytime the timer executes the sinceId must be the 0th element of the array.

Please Guide

It seems like this.myArray is the source your IDs and the which ones you want to include in your requests.

getNewData() {    
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Authorization', token);

    return Observable.timer(60000,60000)
        .switchMap(() => {
            this.myArray.forEach(e => {
                // you can avoid too many vars, they are here just with purpose of clarification
                const sinceId = e.userdataId;
                const dataurl = buildUrl(e.userdataId);
                return this.http.get(dataurl , { headers: headers, method: 'GET' }) ;
            });
        })
        .map((res: Response) => {
            var obj = res.json();
            return obj;
        });
}

buildUrl(sindeId) {
    const catId = this.serv.getSelectedCategory();
    let dataurl = '';

    if (catId < 0) {
        dataurl = `${this.baseUrl}?bookMarked=${isBookmarked}&count=${this.count}&isPrevious=${isPrevious}&sinceId=${sinceId}`;
    }
    else {
        // be careful here, you are hardcoding '/abc'
        dataurl = `${this.baseUrl}company/${catId}/abc?bookMarked=${isBookmarked}&count=${this.count}&isPrevious=${isPrevious}&sinceId=${sinceId}`;
    }

    return dataurl;
}

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