0

Hi i am working on a search service to look for usernames in a elasticsearch database but i got the following error when i want to display the f.e. firstName of the user: Cannot read property 'firstName' of undefined.

I am working with Angular and Elasticsearch

service:

export class SearchService {

  getElastic = 'http://34.62.28.281:9200/users/_doc/_search?q=';
  private handleError: HandleError;

  constructor(
    private http: HttpClient,
    httpErrorHandler: HttpErrorHandler) {
    this.handleError = httpErrorHandler.createHandleError('TimelineService');
  }
  /** GET elasticsearch result */
  getElasticResult( text: string ): Observable<User> {
    this.http.get<User>(this.getElastic + text).subscribe(res => console.log(res));
    return  this.http.get<User>(this.getElastic + text, {responseType: 'json'});

  }

EDIT: The new HTML form:

<form [formGroup]="angForm2" *ngIf="user != null" (ngSubmit)="getUser()" class="form-inline my-5 my-lg-0">
  <input id="searchText" name="searchText" class="form-control" type="string" placeholder="Search for user" aria-label="Post"
         formControlName="searchText" required>
  <p>{{user?.firstName}}</p>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>

the new component:

export class SearchServiceComponent implements OnInit {
  user: User;
  angForm2 = this.fb.group({
    searchText: ['', Validators.required]
  });

  ngOnInit() {
    this.getUser();
    this.getObject();
  }

  constructor(private searchservice: SearchService, private fb: FormBuilder) { }

  getUser() {
    const userName = this.angForm2.value.searchText;
    console.log(this.angForm2.value, userName);
    this.searchservice.getElasticResult(userName).subscribe(user => (this.user = user));
  }
  getObject() {
    return this.user;
  }
}

Output of user and this.user:

enter image description here

User interface:

export interface User {
  $oid: string;
  firstName: string;
  lastName: string;
  username: string;
  password: string;
  follows: User[];
}

3 Answers 3

1

I guess getObject() is getting called when you load the form, by the time user is undefined so you are getting that error. why dont you just use user?.firstName since you already have the variable defined ?

  <p>{{user?.firstName}}</p>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for ur reply, i did change everthing u guys suggested, there is no error anymore but the <p>{{user?.firstname}}</p> is not showing up :/
0

In your html you need to do something like this

<p>{{usersFirstName}}</p>

In your ts code, once you get the response from the server set this usersFirstName.

usersFirstName : string;
getUser() {
    const userName = this.angForm2.value.searchText;
    console.log(this.angForm2.value, userName);
    this.searchservice.getElasticResult(userName).subscribe(user => (this.user = user)
this.usersFirstName = user.name;
);
  }

1 Comment

thanks for ur reply, i did that but still empty <p>
0

1.SearchServiceComponent should implement OnInit class and implement its ngOnInit method 2. Call both the methods inside ngOnInit method sequentially 3. Check if this.user is not equal to null or undefined and handle it using ngIf condition Answered by Sajitharan

Example for OnInit TS

    @Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnInit {
  ngOnInit() {
    this.getUser();
    this.getObject()
  }
getUser() {
    const userName = this.angForm2.value.searchText;
    console.log(this.angForm2.value, userName);
    this.searchservice.getElasticResult(userName).subscribe(user => (this.user = user.hits.hits[0]._source));
  }
  getObject(){}
}

20 Comments

thanks for the reply, i did change the component and the html form. I edited my post with the new code. I don't get the error anymore but there is no output either. the <p>{{user?.firstname}} is not showing up
You haven't called getUser and getObject inside ngOnInit. Please try it and let me know
i did called them but forgot to post the new code ^^ and yeah same thing, not showing up in the <p>
Can you please console.log in below line this.searchservice.getElasticResult(userName).subscribe(user => (this.user = user)); as this.searchservice.getElasticResult(userName).subscribe(user => { console.log(user); console.log(this.user); } )
okay i did that and the console prints all the user data from the elasticsearch database, but still nothing on the <p>user?.firstName</p>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.