0

[I am trying to fetch my email messages through Graph API into the webpart in sharepoint framework]

In the webpart Not getting the details of my emails through Graph API

Error : Cannot read property of 'value' of null

This property of 'value' is defined in the function "this._renderEmailList(messages.value)"

enter image description here

private _renderEmailList(messages: MicrosoftGraph.Message[]): void {
let html: string = '';
for (let index = 0; index < messages.length; index++) {
  html += `<p class="${styles.description}">Email ${index + 1} - ${escape(messages[index].subject)}</p>`;
}

// Add the emails to the placeholder
const listContainer: Element = this.domElement.querySelector('#spListContainer');
listContainer.innerHTML = html;

}

I have call that function in the render function

public render(): void {
this.context.msGraphClientFactory
.getClient()
.then((client: MSGraphClient): void => {
  // get information about the current user from the Microsoft Graph
  client
  .api('/me/messages')
  .top(5)
  .orderby("receivedDateTime desc")
  .get((error, messages: any, rawResponse?: any) => {

    this.domElement.innerHTML = `
    <div class="${ styles.myFirstGraphWebPart}">
    <div class="${ styles.container}">
      <div class="${ styles.row}">
        <div class="${ styles.column}">
          <span class="${ styles.title}">Welcome to SharePoint!</span>
          <p class="${ styles.subTitle}">Use Microsoft Graph in SharePoint Framework.</p>
          <div id="spListContainer" />
        </div>
      </div>
    </div>
    </div>`;

    // List the latest emails based on what we got from the Graph
    this._renderEmailList(messages.value);

  });
});

}

1
  • Check if the variable 'messages' is null.
    – Amos
    Commented Feb 5, 2021 at 7:52

1 Answer 1

0

I think you need to use the full graph URL in your .api call instead of .api("/me/messages") try .api("https://graph.microsoft.com/v1.0/me/messages")

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.