How to return data from a service not a HttpClient to component









up vote
2
down vote

favorite












I have this service and I need to return the products to components I don't use here the HttpClient or Observable as I don't need them



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
return this._wooData.get('products', (err, data, res) =>
return res
);





The code above returns the headers, not the products but if I console the products inside the service itself instead of return I get the products! the code be will like this:



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
console.log(res);
);





The code in the component is just console.log( this._wooService.getAllProducts() ) if I console log at the service



So what am I missing here?










share|improve this question























  • I'm not sure quite what you're asking, but the answer is probably stackoverflow.com/q/6847697/3001761
    – jonrsharpe
    Nov 10 at 18:05











  • I was asking how to return a response from a service that it's not coming from HttpClient module! And @SiddAjmera helped me with that! and I approved his answer you can check it out Anyway thanks for coming over and interesting to helping me out with my question! ♥
    – hesham shawky
    Nov 11 at 2:08














up vote
2
down vote

favorite












I have this service and I need to return the products to components I don't use here the HttpClient or Observable as I don't need them



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
return this._wooData.get('products', (err, data, res) =>
return res
);





The code above returns the headers, not the products but if I console the products inside the service itself instead of return I get the products! the code be will like this:



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
console.log(res);
);





The code in the component is just console.log( this._wooService.getAllProducts() ) if I console log at the service



So what am I missing here?










share|improve this question























  • I'm not sure quite what you're asking, but the answer is probably stackoverflow.com/q/6847697/3001761
    – jonrsharpe
    Nov 10 at 18:05











  • I was asking how to return a response from a service that it's not coming from HttpClient module! And @SiddAjmera helped me with that! and I approved his answer you can check it out Anyway thanks for coming over and interesting to helping me out with my question! ♥
    – hesham shawky
    Nov 11 at 2:08












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have this service and I need to return the products to components I don't use here the HttpClient or Observable as I don't need them



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
return this._wooData.get('products', (err, data, res) =>
return res
);





The code above returns the headers, not the products but if I console the products inside the service itself instead of return I get the products! the code be will like this:



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
console.log(res);
);





The code in the component is just console.log( this._wooService.getAllProducts() ) if I console log at the service



So what am I missing here?










share|improve this question















I have this service and I need to return the products to components I don't use here the HttpClient or Observable as I don't need them



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
return this._wooData.get('products', (err, data, res) =>
return res
);





The code above returns the headers, not the products but if I console the products inside the service itself instead of return I get the products! the code be will like this:



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(
url: 'http://example.com/',
consumerKey: 'key here',
consumerSecret: 'key here',
wpAPI: true,
version: 'wc/v3'
);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
console.log(res);
);





The code in the component is just console.log( this._wooService.getAllProducts() ) if I console log at the service



So what am I missing here?







javascript angular ionic3 angular-services






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:04









jonrsharpe

76.4k11100207




76.4k11100207










asked Nov 10 at 18:02









hesham shawky

365




365











  • I'm not sure quite what you're asking, but the answer is probably stackoverflow.com/q/6847697/3001761
    – jonrsharpe
    Nov 10 at 18:05











  • I was asking how to return a response from a service that it's not coming from HttpClient module! And @SiddAjmera helped me with that! and I approved his answer you can check it out Anyway thanks for coming over and interesting to helping me out with my question! ♥
    – hesham shawky
    Nov 11 at 2:08
















  • I'm not sure quite what you're asking, but the answer is probably stackoverflow.com/q/6847697/3001761
    – jonrsharpe
    Nov 10 at 18:05











  • I was asking how to return a response from a service that it's not coming from HttpClient module! And @SiddAjmera helped me with that! and I approved his answer you can check it out Anyway thanks for coming over and interesting to helping me out with my question! ♥
    – hesham shawky
    Nov 11 at 2:08















I'm not sure quite what you're asking, but the answer is probably stackoverflow.com/q/6847697/3001761
– jonrsharpe
Nov 10 at 18:05





I'm not sure quite what you're asking, but the answer is probably stackoverflow.com/q/6847697/3001761
– jonrsharpe
Nov 10 at 18:05













I was asking how to return a response from a service that it's not coming from HttpClient module! And @SiddAjmera helped me with that! and I approved his answer you can check it out Anyway thanks for coming over and interesting to helping me out with my question! ♥
– hesham shawky
Nov 11 at 2:08




I was asking how to return a response from a service that it's not coming from HttpClient module! And @SiddAjmera helped me with that! and I approved his answer you can check it out Anyway thanks for coming over and interesting to helping me out with my question! ♥
– hesham shawky
Nov 11 at 2:08












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










There are a lot of Appraoches of solving this:



1. Using BehaviorSubject



import BehaviorSubject from 'rxjs';

export class StoreDataProvider

private _wooData: any;
private wooData: BehaviorSubject<any> = new BehaviorSubject<any>(null);
public wooData$ = this.wooData.asObservable();

constructor()
this._wooData = Woo(...);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
this.wooData.next(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.wooData$.subscribe(res => console.log(res));
this._wooService.getAllProducts();



Note that initially you'll get null as we initialized the BehaviorSubject with null. But as soon as you call getAllProducts and receive the data, you're going to get your data.



2. Using Promise.



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
return new Promise((resolve, reject) =>
this._wooData.get('products', (err, data, res) =>
if(err) reject(err);
else resolve(res);
);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts()
.then((res) => console.log(res))



3. Using Callback



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
this._wooData.get('products', (err, data, res) =>
cb(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts((res) => console.log(res));






share|improve this answer






















  • Thanks a lot, bro for your answer it's really helped me ♥
    – hesham shawky
    Nov 10 at 18:48






  • 1




    @heshamshawky, I'm glad it did. Happy Coding. :)
    – SiddAjmera
    Nov 10 at 18:52










  • Can I ask you this I'm trying to learn ionic here! But I got confused by a weird problem that's when using the last angular version 6 or above I'm getting tons of errors when I use this lib ( woocommerce-api ) but if I use angular 5 ( Ionic 3 ) It's working great!!! I did a lot of research about that problem and I found that maybe the 'lib' woocomerce-api using some backend stuff code and that's not available anymore at angular from version +6! So is there a way I can use this module with angular 6 ( ionic 4 )? I'll be glad if you really helped me with this ♥
    – hesham shawky
    Nov 11 at 2:14










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',
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%2f53241889%2fhow-to-return-data-from-a-service-not-a-httpclient-to-component%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










There are a lot of Appraoches of solving this:



1. Using BehaviorSubject



import BehaviorSubject from 'rxjs';

export class StoreDataProvider

private _wooData: any;
private wooData: BehaviorSubject<any> = new BehaviorSubject<any>(null);
public wooData$ = this.wooData.asObservable();

constructor()
this._wooData = Woo(...);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
this.wooData.next(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.wooData$.subscribe(res => console.log(res));
this._wooService.getAllProducts();



Note that initially you'll get null as we initialized the BehaviorSubject with null. But as soon as you call getAllProducts and receive the data, you're going to get your data.



2. Using Promise.



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
return new Promise((resolve, reject) =>
this._wooData.get('products', (err, data, res) =>
if(err) reject(err);
else resolve(res);
);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts()
.then((res) => console.log(res))



3. Using Callback



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
this._wooData.get('products', (err, data, res) =>
cb(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts((res) => console.log(res));






share|improve this answer






















  • Thanks a lot, bro for your answer it's really helped me ♥
    – hesham shawky
    Nov 10 at 18:48






  • 1




    @heshamshawky, I'm glad it did. Happy Coding. :)
    – SiddAjmera
    Nov 10 at 18:52










  • Can I ask you this I'm trying to learn ionic here! But I got confused by a weird problem that's when using the last angular version 6 or above I'm getting tons of errors when I use this lib ( woocommerce-api ) but if I use angular 5 ( Ionic 3 ) It's working great!!! I did a lot of research about that problem and I found that maybe the 'lib' woocomerce-api using some backend stuff code and that's not available anymore at angular from version +6! So is there a way I can use this module with angular 6 ( ionic 4 )? I'll be glad if you really helped me with this ♥
    – hesham shawky
    Nov 11 at 2:14














up vote
1
down vote



accepted










There are a lot of Appraoches of solving this:



1. Using BehaviorSubject



import BehaviorSubject from 'rxjs';

export class StoreDataProvider

private _wooData: any;
private wooData: BehaviorSubject<any> = new BehaviorSubject<any>(null);
public wooData$ = this.wooData.asObservable();

constructor()
this._wooData = Woo(...);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
this.wooData.next(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.wooData$.subscribe(res => console.log(res));
this._wooService.getAllProducts();



Note that initially you'll get null as we initialized the BehaviorSubject with null. But as soon as you call getAllProducts and receive the data, you're going to get your data.



2. Using Promise.



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
return new Promise((resolve, reject) =>
this._wooData.get('products', (err, data, res) =>
if(err) reject(err);
else resolve(res);
);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts()
.then((res) => console.log(res))



3. Using Callback



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
this._wooData.get('products', (err, data, res) =>
cb(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts((res) => console.log(res));






share|improve this answer






















  • Thanks a lot, bro for your answer it's really helped me ♥
    – hesham shawky
    Nov 10 at 18:48






  • 1




    @heshamshawky, I'm glad it did. Happy Coding. :)
    – SiddAjmera
    Nov 10 at 18:52










  • Can I ask you this I'm trying to learn ionic here! But I got confused by a weird problem that's when using the last angular version 6 or above I'm getting tons of errors when I use this lib ( woocommerce-api ) but if I use angular 5 ( Ionic 3 ) It's working great!!! I did a lot of research about that problem and I found that maybe the 'lib' woocomerce-api using some backend stuff code and that's not available anymore at angular from version +6! So is there a way I can use this module with angular 6 ( ionic 4 )? I'll be glad if you really helped me with this ♥
    – hesham shawky
    Nov 11 at 2:14












up vote
1
down vote



accepted







up vote
1
down vote



accepted






There are a lot of Appraoches of solving this:



1. Using BehaviorSubject



import BehaviorSubject from 'rxjs';

export class StoreDataProvider

private _wooData: any;
private wooData: BehaviorSubject<any> = new BehaviorSubject<any>(null);
public wooData$ = this.wooData.asObservable();

constructor()
this._wooData = Woo(...);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
this.wooData.next(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.wooData$.subscribe(res => console.log(res));
this._wooService.getAllProducts();



Note that initially you'll get null as we initialized the BehaviorSubject with null. But as soon as you call getAllProducts and receive the data, you're going to get your data.



2. Using Promise.



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
return new Promise((resolve, reject) =>
this._wooData.get('products', (err, data, res) =>
if(err) reject(err);
else resolve(res);
);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts()
.then((res) => console.log(res))



3. Using Callback



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
this._wooData.get('products', (err, data, res) =>
cb(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts((res) => console.log(res));






share|improve this answer














There are a lot of Appraoches of solving this:



1. Using BehaviorSubject



import BehaviorSubject from 'rxjs';

export class StoreDataProvider

private _wooData: any;
private wooData: BehaviorSubject<any> = new BehaviorSubject<any>(null);
public wooData$ = this.wooData.asObservable();

constructor()
this._wooData = Woo(...);


getAllProducts()
this._wooData.get('products', (err, data, res) =>
this.wooData.next(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.wooData$.subscribe(res => console.log(res));
this._wooService.getAllProducts();



Note that initially you'll get null as we initialized the BehaviorSubject with null. But as soon as you call getAllProducts and receive the data, you're going to get your data.



2. Using Promise.



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
return new Promise((resolve, reject) =>
this._wooData.get('products', (err, data, res) =>
if(err) reject(err);
else resolve(res);
);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts()
.then((res) => console.log(res))



3. Using Callback



export class StoreDataProvider 

private _wooData: any;

constructor()
this._wooData = Woo(...);


getAllProducts(cb)
this._wooData.get('products', (err, data, res) =>
cb(res);
);





And then you can use it in your Component like this:



constructor(private _wooService: WooService) 

ngOnInit()
this._wooService.getAllProducts((res) => console.log(res));







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 18:35

























answered Nov 10 at 18:29









SiddAjmera

12.1k21137




12.1k21137











  • Thanks a lot, bro for your answer it's really helped me ♥
    – hesham shawky
    Nov 10 at 18:48






  • 1




    @heshamshawky, I'm glad it did. Happy Coding. :)
    – SiddAjmera
    Nov 10 at 18:52










  • Can I ask you this I'm trying to learn ionic here! But I got confused by a weird problem that's when using the last angular version 6 or above I'm getting tons of errors when I use this lib ( woocommerce-api ) but if I use angular 5 ( Ionic 3 ) It's working great!!! I did a lot of research about that problem and I found that maybe the 'lib' woocomerce-api using some backend stuff code and that's not available anymore at angular from version +6! So is there a way I can use this module with angular 6 ( ionic 4 )? I'll be glad if you really helped me with this ♥
    – hesham shawky
    Nov 11 at 2:14
















  • Thanks a lot, bro for your answer it's really helped me ♥
    – hesham shawky
    Nov 10 at 18:48






  • 1




    @heshamshawky, I'm glad it did. Happy Coding. :)
    – SiddAjmera
    Nov 10 at 18:52










  • Can I ask you this I'm trying to learn ionic here! But I got confused by a weird problem that's when using the last angular version 6 or above I'm getting tons of errors when I use this lib ( woocommerce-api ) but if I use angular 5 ( Ionic 3 ) It's working great!!! I did a lot of research about that problem and I found that maybe the 'lib' woocomerce-api using some backend stuff code and that's not available anymore at angular from version +6! So is there a way I can use this module with angular 6 ( ionic 4 )? I'll be glad if you really helped me with this ♥
    – hesham shawky
    Nov 11 at 2:14















Thanks a lot, bro for your answer it's really helped me ♥
– hesham shawky
Nov 10 at 18:48




Thanks a lot, bro for your answer it's really helped me ♥
– hesham shawky
Nov 10 at 18:48




1




1




@heshamshawky, I'm glad it did. Happy Coding. :)
– SiddAjmera
Nov 10 at 18:52




@heshamshawky, I'm glad it did. Happy Coding. :)
– SiddAjmera
Nov 10 at 18:52












Can I ask you this I'm trying to learn ionic here! But I got confused by a weird problem that's when using the last angular version 6 or above I'm getting tons of errors when I use this lib ( woocommerce-api ) but if I use angular 5 ( Ionic 3 ) It's working great!!! I did a lot of research about that problem and I found that maybe the 'lib' woocomerce-api using some backend stuff code and that's not available anymore at angular from version +6! So is there a way I can use this module with angular 6 ( ionic 4 )? I'll be glad if you really helped me with this ♥
– hesham shawky
Nov 11 at 2:14




Can I ask you this I'm trying to learn ionic here! But I got confused by a weird problem that's when using the last angular version 6 or above I'm getting tons of errors when I use this lib ( woocommerce-api ) but if I use angular 5 ( Ionic 3 ) It's working great!!! I did a lot of research about that problem and I found that maybe the 'lib' woocomerce-api using some backend stuff code and that's not available anymore at angular from version +6! So is there a way I can use this module with angular 6 ( ionic 4 )? I'll be glad if you really helped me with this ♥
– hesham shawky
Nov 11 at 2:14

















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53241889%2fhow-to-return-data-from-a-service-not-a-httpclient-to-component%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo