-1
ngOnInit() {
  this.encrypted = this.enCryption(Token, data); // here this.encrypted getting null
 }

 public enCryption(token, data){
    return this.encryptionService.set(key, data).subscribe(data=>{});
 }

i want to reuse this enCryption() function and i dont want no function executed this (bottom) way

 this.encryptionService.set(key, data).subscribe(data=>{
 this.anotherfunction(data);
 Or
 this.encrypted = data
 })
Sanoj Sen
  • 17
  • 1
  • 1
  • 3
  • Possible duplicate of [How do I return the response from an Observable/http/async call in angular?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular) – Harun Yilmaz Apr 04 '19 at 11:16

1 Answers1

0

try this

ngOnInit() {
  this.encrypted = this.enCryption(Token, data); // here this.encrypted getting null
 }

 public enCryption(token, data){
    return this.encryptionService.set(key, data).map(data=> data.json());
 }
Niraj
  • 775
  • 7
  • 20
  • core.js:14597 ERROR TypeError: this.encdecService.set(...).map is not a function at SupportMainComponent.push../src/app/support-main/support-main.component.ts.SupportMainComponent.enCrypt1 (support-main.component.ts:73) – Sanoj Sen Apr 04 '19 at 11:40
  • Here is my encryption service public set(keys, value):any{ const Encrpt = new Observable(observer => { console.log(value) var key = CryptoJS.enc.Utf8.parse(keys); var iv = CryptoJS.enc.Utf8.parse(IV'); var encrypted = CryptoJS.AES.encrypt(JSON.stringify(value), key, { keySize: 256 / 32, iv: iv, mode: CryptoJS.mode.CBC }); observer.next(encrypted.toString(CryptoJS.format.Hex)); }) return Encrpt; } – Sanoj Sen Apr 04 '19 at 11:41
  • After encryption im getting a Hex formatted string., does this work with " .map(data=> data.json())" method ? – Sanoj Sen Apr 04 '19 at 11:43
  • please add a reference for map operator using rx.js like import {map} from "rxjs/operators"; in your component and you will get your string back – Niraj Apr 04 '19 at 11:47