SharePoint / sp-dev-docs

SharePoint & Viva Connections Developer Documentation
https://docs.microsoft.com/en-us/sharepoint/dev/
Creative Commons Attribution 4.0 International
1.24k stars 1k forks source link

Cannot read property 'getClient' of undefined error - spfx Microsoft Graph #9817

Open ChrisMRo opened 1 month ago

ChrisMRo commented 1 month ago

Target SharePoint environment

SharePoint Online

What SharePoint development model, framework, SDK or API is this about?

πŸ’₯ SharePoint Framework

Developer environment

None

What browser(s) / client(s) have you tested

Additional environment details

Describe the bug / error

I have been trying to use MS Graph in my spfx project but get the following error:

'Cannot read properties of undefined (reading 'getClient')' when running gulp serve.

I am using the following Microsoft template for the project https://learn.microsoft.com/en-us/sharepoint/dev/spfx/use-msgraph

I have followed the tutorial but have got nowhere trying to find a solution to this.

I am using React with the most recent version of the SharePoint framework installed on a new VM.

I have placed the following in the public render() function as shown in the tutorial:

this.context.msGraphClientFactory .getClient('3') .then((client: MSGraphClientV3): void => { client .api('/me') .get((error: any, user: MicrosoftGraph.User, rawResponse?: any) => { // handle the response });

Once I comment out the getClient('3'), the project builds successfully and loads with no errors.

Any help greatly appreciated! I've been lost in this for the last 3 days!

Steps to reproduce

I followed the steps in this Microsoft guide

https://learn.microsoft.com/en-us/sharepoint/dev/spfx/use-msgraph

Expected behavior

When running gulp serve I would expect the solution to build and to see logged on user properties.

Identical issue reported here https://github.com/SharePoint/sp-dev-docs/issues/6146

Tanddant commented 1 month ago

If you place a debugger, and have a look at your this.context, what does it contain? - my guess is that you might've forgotten to pass in your SPFx Context, remember TypeScipt only gives you compile time safety, not actual runtime.