0

I am trying below code but always get undefined in console for console.log(responseJSON.Title);

this.props.context.spHttpClient.get(`https://someazuresite.azurewebsites.net/api`,  
      SPHttpClient.configurations.v1)  
      .then((response: SPHttpClientResponse) => {  
        return  response.json().then((responseJSON: any) => { 

          console.log(responseJSON.Title);  
        });  
      }); 

[enter image description here]

5
  • What is the output when you log responseJSON? Is it an object or array? Mar 8, 2023 at 13:01
  • Is this API secured using Azure AD app? Mar 8, 2023 at 13:07
  • its an array like [Title:"",id:""]
    – kumar
    Mar 8, 2023 at 13:43
  • Array cannot be created like this. Simple array is like: ["Saab", "Volvo", "BMW"]; and array of objects is like: [{Title:"Title 1",id:"1"}, {Title:"Title 2",id:"2"}]. Can you add screenshot of response to your original question? Mar 8, 2023 at 13:52
  • added screebshot and response is same as [{Title:"Title 1",id:"1"}, {Title:"Title 2",id:"2"}]
    – kumar
    Mar 8, 2023 at 14:04

1 Answer 1

1

As your responseJSON is an array, you have to loop through array to get property values.

In place of console.log(responseJSON.Title);, you can use:

for (let index = 0; index < responseJSON.length; index++){
    console.log(responseJSON[index].Title);
}

You can find other ways like using forEach, map at:

  1. How to loop through an array containing objects and access their properties
  2. Loop through an array in JavaScript

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.