0
votes

I make service which encoding password. this service use ts-md5:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import { Md5 } from 'ts-md5/dist/md5';


@Injectable()
export class HashService {

  constructor() { }

  generate(str): Observable<string>{
    const h = Md5.hashStr(str);
    console.log(h, typeof h);
    return h;
  }

}

From component i subscribe to service:

this.hashService.generate(this.form.value.password).subscribe((hash) => {
  console.log(hash);
});

But console display follow error message:

ERROR in src/app/shared/services/hash.service.ts(15,5): error TS2322: Type 'string | Int32Array' is not assignable to type 'Observable'. Type 'string' is not assignable to type 'Observable'

I tried to specify a more common type:

generate(str): Observable<any>{

but the problem persists

1

1 Answers

2
votes

Md5.hashStr() does not return an Observable, it returns a string. You do not need to return an observable and subscribe. You can simply return the string

// service.ts
generate(str): string {
    const h = Md5.hashStr(str);
    return h;
}

// component.ts
this.hash = this.hashService.generate(this.form.value.password);