0

i want to know how i can save the json array from my GET Request on a variable. I am working on a search service and can already search for user but also want to search for Moods (similar to Tweets).

This sends a GET request to the elastic db and requestes MoodTexts(Posts with Text and emoji). If i search for "green", my request finds 4 results in the database and my searchbar in the html shows 4 blank results, but i want to post the found text in those blank fields. Btw Mood is a class with the variable "text".

Component (i think here is the mistake):

mood: Mood[];

getMood() {
    const moodText = this.angForm2.value.searchText;
    console.log(this.angForm2.value, moodText);
    this.searchservice.getElasticPostsResult(moodText).subscribe((data2: any) => {
      this.mood = data2.hits.hits;
    });
  }

HTML:

  <div id="textSearch" *ngFor="let moodText of mood">
    <br><a id="a2">{{moodText?.text}}</a>
  </div>

Console/Request output:

enter image description here

2
  • What is the error you get? In the JSON output there is no text data ... Which data are you trying to access? Commented Jun 3, 2020 at 16:40
  • In the json is under f.e. hits.hits[0]._source is a variable "text" i want to output that on my searchbar results. The error i get is "Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays." Commented Jun 3, 2020 at 16:50

2 Answers 2

1

Try this.

<div id="textSearch" *ngFor="let moodText of mood">
    <br><a id="a2">{{moodText['_source']?.text}}</a>
  </div>
Sign up to request clarification or add additional context in comments.

Comments

0

In order to access mood you can use the below way.

<div id="textSearch" *ngFor="let moodText of mood">
    <br><a id="a2">{{moodText?._source.text}}</a>
  </div>

The text is coming inside the _source object.

Comments

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.