Home > front end >  Property length does not exist on type Observable <[]> in angular
Property length does not exist on type Observable <[]> in angular

Time:02-10

I have this service in angular which returns transcript

public transcript_arr =new ReplaySubject<[]>(1);
public confidence_arr=new ReplaySubject<[]>(1);
getTranscriptValue()
  {
    return this.transcript_arr.pipe(data => {return data;})
  }


getConfidenceValue()
  {
    return this.confidence_arr;
  }

.ts file

service_transcript$: Observable<[]>;

this.service_transcript$=this.service.getTranscriptValue();

 for (var i = 0; i < this.service_transcript$.length; i  ) {

 }

 this.http.post(this.baseUrl 'api/auth/post-data', formData).subscribe(response  => {

}

In .ts file i'm using service data for computational purpose then sending it to server but getting this error :

Property length does not exist on type Observable <[]> in angular Updated Code:

 const transcript_arr = [];
 const confidence_arr = [];

      this.service.getTranscriptValue().subscribe(el=>{
        for (var i = 0; i < el.length; i  ) {
            if(this.hasWhiteSpace(el[i]))
            {     
              const arr_transcript = el[i].trim().split(" "); 
              for (var j = 0; j < arr_transcript.length; j  ) {
                transcript_arr.push(arr_transcript[j].toLowerCase());
                confidence_arr.push(service_confidence[i]);
              } 
            }
            else
            {
              transcript_arr.push(el[i].toLowerCase());
              confidence_arr.push(service_confidence[i]);
            }
        }
      }) ; 

Any solution to resolve this issue Thanks

CodePudding user response:

The pipe in your getTranscriptValue doesn't do anything, you can safely remove it altogether:

getTranscriptValue() {
  return this.transcript_arr;
}

As for the for loop, you need to get access to the actual data by subscribing to your Observable and then loop through that. Something like this:

this.service_transcript$.subscribe(transcripts => {
  for (var i = 0; i < transcripts.length; i  ) {
     
  }
});

CodePudding user response:

You are trying to get the length of observable, not the content of that observable you should try below code, Because this.service_transcript$ is observable and observable contains data which you need. so you can try this code.


this.service_transcript$.subscribe(res =>{

   for (var i = 0; i < response.length; i  ) {
      // Your computation
   }
   this.http.post(this.baseUrl 'api/auth/post-data',formData).subscribe(response  => { });

 })

CodePudding user response:

You have observable element. You cant check length there. You need to subscribe your element and inside it you should do your process

 this.service.getTranscriptValue().subscribe(el=>{
    for (var i = 0; i < el.length; i  ) {
    }
  }) ; 
  • Related