Using the httpModule
from an API to retrieve data is my current task. I have included my code snippet below:
async searchMeaning(form: NgForm) {
const post = {
word: form.value.inputWord,
language: form.value.language
}
console.log(post);
if (post.language && post.word) {
this.output1 = await this.callApi(post); // it displays await has not effect
console.log(this.output1) // undefined.
}
}
callApi(post) {
this.http.get('https://api.dictionaryapi.dev/api/v2/entries/'+post.language+'/'+post.word)
.subscribe((data) => {
console.log(JSON.parse(JSON.stringify(data)));
return data;
}, (error : any) => {
return error
})
}
My issue arises when attempting to utilize async
and await
; an "await has no effect" warning is triggered. Additionally, the variable this.output
is being assigned as undefined
. How can these issues be resolved? Furthermore, how can I access a variable from the response array provided below?
[
{
"word": "hello",
"phonetics": [
{
"text": "/həˈloʊ/",
"audio": "https://lex-audio.useremarkable.com/mp3/hello_us_1_rr.mp3"
},
{
"text": "/hɛˈloʊ/",
"audio": "https://lex-audio.useremarkable.com/mp3/hello_us_2_rr.mp3"
}
],
"meanings": [
{
"partOfSpeech": "exclamation",
"definitions": [
{
"definition": "Used as a greeting or to begin a phone conversation.",
"example": "hello there, Katie!"
}
]
},
{
"partOfSpeech": "noun",
"definitions": [
{
"definition": "An utterance of “hello”; a greeting.",
"example": "she was getting polite nods and hellos from people",
"synonyms": [
"greeting",
"welcome",
"salutation",
"saluting",
"hailing",
"address",
"hello",
"hallo"
]
}
]
},
{
"partOfSpeech": "intransitive verb",
"definitions": [
{
"definition": "Say or shout “hello”; greet someone.",
"example": "I pressed the phone button and helloed"
}
]
}
]
} ]
I am specifically interested in extracting the value of the definition
variable from the above array. How can this be achieved?