1
export class PropertiesService {
 getCommonProperties():any{
        let URL: string = "/xxxx/yyyyy";
        let headers = new Headers();
        let options = new RequestOptions({ headers });
        options.method = GET;
        return this.http.request(URL, options).map(response => {
            {
                console.log("response ", response.json());
                return response.json();
            };
    }
}

/**************************/
export class A {

this.commonProperties.setPropertiesMap(
this.propertiesService.getCommonProperties().subscribe(result => 
{ return result;})
);
//logic to iterate commonPropertiesMap
}

/*PropertiesMap is a map which stores a key value pair of some properties,I have a service named propertiesService where i get the stream of properties from the server, and i have to set the result into properties map. I am getting the result from server(i could see it in the console ) where as the control is passing to the next step even before i get the response from the server, so there is an error thrown that the PropertiesMap is not iterable. Can someone help me with the logic to hold the control until i get the result from HTTP request and set the values into the PropertiesMap and then iterate through the map?? */

teja sri
  • 11
  • 5
  • 2
    Put anything that relies on the result within the `subscribe` function, or a function that's called from within the subscribe function – user184994 Jul 30 '18 at 17:32
  • Sorry I didn't understand could you please be more elaborate? – teja sri Jul 30 '18 at 17:37
  • I've added an answer below. Im not entirely sure what the `setPropertiesMap` function expects, but you should get the general idea – user184994 Jul 30 '18 at 17:41
  • Put your logic inside your subscribe that attains the result: this.propertiesService.getCommonProperties().subscribe(result => // Do stuff with result; //logic to iterate commonPropertiesMap } ) ); – Taranjit Kang Jul 30 '18 at 19:50

2 Answers2

1

Put anything that relies on the result within the subscribe function, or a function that's called from within the subscribe function, like so:

this.propertiesService.getCommonProperties().subscribe(result => {
    // Do you logic that relies on the results within here
    this.commonProperties.setPropertiesMap(result);
    return result;
})
user184994
  • 17,791
  • 1
  • 46
  • 52
  • /*I have changed the code but no luck*/ public getCommonProperties(): CommonProperties { if (this.commonProperties == null) { this.commonProperties = new commonProperties(); this.propertiesService.getCommonProperties().subscribe(result => { this.commonProperties.commonPropertiesMap = result; }); console.log("map", this.commonProperties.commonPropertiesMap); } return this.commonProperties; } – teja sri Jul 30 '18 at 17:43
  • @tejasri Because the code is asynchronous, your return value will always be `undefined`. – user184994 Jul 30 '18 at 17:47
  • got it thank you so much – teja sri Jul 30 '18 at 17:49
0

you have to put the logic to iterate with in subscribe not outside

this.propertiesService.getCommonProperties().subscribe(result => 
this.commonProperties.setPropertiesMap();
//logic to iterate commonPropertiesMap
return result;
);
Kumar Garapati
  • 619
  • 1
  • 10
  • 24