1

I have been using this line previously, and suddenly it does not work.

private  getListData() {
    const opt: ISPHttpClientOptions = { headers: { 'Content-Type': 'application/json;odata=verbose' } };
    this.props.SPHttpClient.get(this.props.pagecontext.web.absoluteUrl + "/_api/web/lists/getbytitle('News')/items?$select=*,AttachmentFiles&$expand=AttachmentFiles/Title&$orderby=ID desc", SPHttpClient.configurations.v1, opt).then((response: SPHttpClientResponse) => {
      response.json().then((json: any) => {
         for(let i=0;i<json.value.length;i++){
          var url=this.geturl(json.value[i].ID);

            this.data.push({ID:<td>{json.value[i].ID}</td>,
             Title:<td><a href={url} target="_blank">{json.value[i].Title}</a></td>,
             Url:<td><img src={json.value[i].AttachmentFiles[0].ServerRelativeUrl} width="300px" height="300px" /></td>
              });
             //   debugger;

1 Answer 1

0

If the attachment is not uploaded for the list item then AttachmentFiles[0] will be undefined. So you need to use it like below:

this.data.push({
                  ID:<td>{json.value[i].ID}</td>,
                  Title:<td><a href={url} target="_blank">{json.value[i].Title}</a></td>,
                  Url:<td><img src={json.value[i].AttachmentFiles[0] ? json.value[i].AttachmentFiles[0].ServerRelativeUrl: ''} width="300px" height="300px" /></td>
              });
2

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.