Want to return new observable with pipe, with new data passed in each time. With Angular 6












1















I have a get method that retrieves data from an API with values that changes every minute. This data needs to be multiplied with static data every minute and displayed back to the user . I have decided to use the pipe to handle this transaction since this data will be used frequently throughout the app itself.



My issue is that when implementing this pipe, running in a loop it takes the last static value of that was piped in and changes it for all values that have already passed.



I want the multiplied value for the individual static value that is passed. and returned back to the UI as an observable.



@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(private service: AppService,private http: HttpClient) {}
private fetchDataTemp$: Observable<any>;

private killTrigger: Subject<void> = new Subject();

private refreshIntervalTemp$: Observable<any> = timer(0, 60000)
.pipe(
// This kills the request if the user closes the component
takeUntil(this.killTrigger),
// switchMap cancels the last request, if no response have been received since last tick
switchMap(() => this.fetchDataTemp$),
// catchError handles http throws
catchError(error => of('0'))
);

getData() {
return this.http.get("https://jsonplaceholder.typicode.com/posts/42")
.pipe(
retry(3),
share()
);
}

public tempdata$: Observable<any> = this.refreshIntervalTemp$;
fakedata = [1,2,3];

ngOnInit() {
this.fakedata.forEach(element => {
this.tempdata$.subscribe(data => { console.log(data,"test"); });
console.log(element);

this.fetchDataTemp$ = this.getPrice().pipe(
map(
(response) => {
console.log(element*response.id as number)
return response.id;
},
share()
)
);
});
}
}


The code above is very similar to the scenario to what I want to achieve.
the fakedata is the data passed into the pipe, and the foreach is the ngFor loop running with async. the url https://jsonplaceholder.typicode.com/posts/42
returns static data in this case, so I am able to keep data consistent response.id is 42. when running this code the results are



1
2
3
126 test
126 test
126 test


What I expect is



 1
2
3
42 test
84 test
126 test


I pinpointed the issue to the map() but I don't know how to force the map to handle values one at a time and return them accordingly.



Thanks in advance!










share|improve this question

























  • Just out of curiosity, what happens if you remove this.fetchDataTemp$ = before calling getPrice?

    – Michael Beeson
    Nov 13 '18 at 19:06











  • this.fetchDataTemp$ is used in the timer, and those results are passed back to the timer, where the timer returns the results totempdata$, so an error of undefined will be thrown. since the timer expects a stream initialized.

    – Gunth
    Nov 13 '18 at 19:19
















1















I have a get method that retrieves data from an API with values that changes every minute. This data needs to be multiplied with static data every minute and displayed back to the user . I have decided to use the pipe to handle this transaction since this data will be used frequently throughout the app itself.



My issue is that when implementing this pipe, running in a loop it takes the last static value of that was piped in and changes it for all values that have already passed.



I want the multiplied value for the individual static value that is passed. and returned back to the UI as an observable.



@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(private service: AppService,private http: HttpClient) {}
private fetchDataTemp$: Observable<any>;

private killTrigger: Subject<void> = new Subject();

private refreshIntervalTemp$: Observable<any> = timer(0, 60000)
.pipe(
// This kills the request if the user closes the component
takeUntil(this.killTrigger),
// switchMap cancels the last request, if no response have been received since last tick
switchMap(() => this.fetchDataTemp$),
// catchError handles http throws
catchError(error => of('0'))
);

getData() {
return this.http.get("https://jsonplaceholder.typicode.com/posts/42")
.pipe(
retry(3),
share()
);
}

public tempdata$: Observable<any> = this.refreshIntervalTemp$;
fakedata = [1,2,3];

ngOnInit() {
this.fakedata.forEach(element => {
this.tempdata$.subscribe(data => { console.log(data,"test"); });
console.log(element);

this.fetchDataTemp$ = this.getPrice().pipe(
map(
(response) => {
console.log(element*response.id as number)
return response.id;
},
share()
)
);
});
}
}


The code above is very similar to the scenario to what I want to achieve.
the fakedata is the data passed into the pipe, and the foreach is the ngFor loop running with async. the url https://jsonplaceholder.typicode.com/posts/42
returns static data in this case, so I am able to keep data consistent response.id is 42. when running this code the results are



1
2
3
126 test
126 test
126 test


What I expect is



 1
2
3
42 test
84 test
126 test


I pinpointed the issue to the map() but I don't know how to force the map to handle values one at a time and return them accordingly.



Thanks in advance!










share|improve this question

























  • Just out of curiosity, what happens if you remove this.fetchDataTemp$ = before calling getPrice?

    – Michael Beeson
    Nov 13 '18 at 19:06











  • this.fetchDataTemp$ is used in the timer, and those results are passed back to the timer, where the timer returns the results totempdata$, so an error of undefined will be thrown. since the timer expects a stream initialized.

    – Gunth
    Nov 13 '18 at 19:19














1












1








1








I have a get method that retrieves data from an API with values that changes every minute. This data needs to be multiplied with static data every minute and displayed back to the user . I have decided to use the pipe to handle this transaction since this data will be used frequently throughout the app itself.



My issue is that when implementing this pipe, running in a loop it takes the last static value of that was piped in and changes it for all values that have already passed.



I want the multiplied value for the individual static value that is passed. and returned back to the UI as an observable.



@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(private service: AppService,private http: HttpClient) {}
private fetchDataTemp$: Observable<any>;

private killTrigger: Subject<void> = new Subject();

private refreshIntervalTemp$: Observable<any> = timer(0, 60000)
.pipe(
// This kills the request if the user closes the component
takeUntil(this.killTrigger),
// switchMap cancels the last request, if no response have been received since last tick
switchMap(() => this.fetchDataTemp$),
// catchError handles http throws
catchError(error => of('0'))
);

getData() {
return this.http.get("https://jsonplaceholder.typicode.com/posts/42")
.pipe(
retry(3),
share()
);
}

public tempdata$: Observable<any> = this.refreshIntervalTemp$;
fakedata = [1,2,3];

ngOnInit() {
this.fakedata.forEach(element => {
this.tempdata$.subscribe(data => { console.log(data,"test"); });
console.log(element);

this.fetchDataTemp$ = this.getPrice().pipe(
map(
(response) => {
console.log(element*response.id as number)
return response.id;
},
share()
)
);
});
}
}


The code above is very similar to the scenario to what I want to achieve.
the fakedata is the data passed into the pipe, and the foreach is the ngFor loop running with async. the url https://jsonplaceholder.typicode.com/posts/42
returns static data in this case, so I am able to keep data consistent response.id is 42. when running this code the results are



1
2
3
126 test
126 test
126 test


What I expect is



 1
2
3
42 test
84 test
126 test


I pinpointed the issue to the map() but I don't know how to force the map to handle values one at a time and return them accordingly.



Thanks in advance!










share|improve this question
















I have a get method that retrieves data from an API with values that changes every minute. This data needs to be multiplied with static data every minute and displayed back to the user . I have decided to use the pipe to handle this transaction since this data will be used frequently throughout the app itself.



My issue is that when implementing this pipe, running in a loop it takes the last static value of that was piped in and changes it for all values that have already passed.



I want the multiplied value for the individual static value that is passed. and returned back to the UI as an observable.



@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(private service: AppService,private http: HttpClient) {}
private fetchDataTemp$: Observable<any>;

private killTrigger: Subject<void> = new Subject();

private refreshIntervalTemp$: Observable<any> = timer(0, 60000)
.pipe(
// This kills the request if the user closes the component
takeUntil(this.killTrigger),
// switchMap cancels the last request, if no response have been received since last tick
switchMap(() => this.fetchDataTemp$),
// catchError handles http throws
catchError(error => of('0'))
);

getData() {
return this.http.get("https://jsonplaceholder.typicode.com/posts/42")
.pipe(
retry(3),
share()
);
}

public tempdata$: Observable<any> = this.refreshIntervalTemp$;
fakedata = [1,2,3];

ngOnInit() {
this.fakedata.forEach(element => {
this.tempdata$.subscribe(data => { console.log(data,"test"); });
console.log(element);

this.fetchDataTemp$ = this.getPrice().pipe(
map(
(response) => {
console.log(element*response.id as number)
return response.id;
},
share()
)
);
});
}
}


The code above is very similar to the scenario to what I want to achieve.
the fakedata is the data passed into the pipe, and the foreach is the ngFor loop running with async. the url https://jsonplaceholder.typicode.com/posts/42
returns static data in this case, so I am able to keep data consistent response.id is 42. when running this code the results are



1
2
3
126 test
126 test
126 test


What I expect is



 1
2
3
42 test
84 test
126 test


I pinpointed the issue to the map() but I don't know how to force the map to handle values one at a time and return them accordingly.



Thanks in advance!







angular angular2-observables angular-pipe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 20:35









Daniel W Strimpel

3,4161617




3,4161617










asked Nov 13 '18 at 18:55









GunthGunth

63




63













  • Just out of curiosity, what happens if you remove this.fetchDataTemp$ = before calling getPrice?

    – Michael Beeson
    Nov 13 '18 at 19:06











  • this.fetchDataTemp$ is used in the timer, and those results are passed back to the timer, where the timer returns the results totempdata$, so an error of undefined will be thrown. since the timer expects a stream initialized.

    – Gunth
    Nov 13 '18 at 19:19



















  • Just out of curiosity, what happens if you remove this.fetchDataTemp$ = before calling getPrice?

    – Michael Beeson
    Nov 13 '18 at 19:06











  • this.fetchDataTemp$ is used in the timer, and those results are passed back to the timer, where the timer returns the results totempdata$, so an error of undefined will be thrown. since the timer expects a stream initialized.

    – Gunth
    Nov 13 '18 at 19:19

















Just out of curiosity, what happens if you remove this.fetchDataTemp$ = before calling getPrice?

– Michael Beeson
Nov 13 '18 at 19:06





Just out of curiosity, what happens if you remove this.fetchDataTemp$ = before calling getPrice?

– Michael Beeson
Nov 13 '18 at 19:06













this.fetchDataTemp$ is used in the timer, and those results are passed back to the timer, where the timer returns the results totempdata$, so an error of undefined will be thrown. since the timer expects a stream initialized.

– Gunth
Nov 13 '18 at 19:19





this.fetchDataTemp$ is used in the timer, and those results are passed back to the timer, where the timer returns the results totempdata$, so an error of undefined will be thrown. since the timer expects a stream initialized.

– Gunth
Nov 13 '18 at 19:19












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287776%2fwant-to-return-new-observable-with-pipe-with-new-data-passed-in-each-time-with%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287776%2fwant-to-return-new-observable-with-pipe-with-new-data-passed-in-each-time-with%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues