0

I've learned a lot about it, but I didn't figure it out why I got this error:

Only valid in async functions and the top level bodies of modules (at index.js:13:21)

That's the code:

async function run() {
    console.log("running...");


    const config = {
        auth: {
            clientId: 'xxxxxxxxxxxxxxxxxxxx',
            authority: 'https://login.microsoftonline.com/organizations/',
            redirectUri: 'http://localhost:8080'
        }
    };
    var client = new msal.PublicClientApplication(config);
    
    var loginRequest = {
        scopes: [ 'user.read' ]
    };
    let loginResponse = await client.loginPopup(loginRequest);
    console.log('Login Response', loginResponse);

}

The run function is called from front-end page:

<button type="button" class="btn btn-primary btn-lg" onclick="run()">
       <i class="fa fa-fw fa-inverse fa-play"></i>
       Run Code
</button>
S_A
  • 411
  • 1
  • 7
  • 25
  • 1
    Hello, can you share the main code of the `run()` function call ? – sohaieb azaiez May 17 '23 at 18:09
  • 1
    thank you ! I have two questions: - is `run()` function defined in nodeJs environment ? or in a separate frontend script file ? - if it is the second case (separate frontend script): did you try to simulate click event with addEventListener instead of using it directly in Html Dom events ? PS: remember that your function is defined as `async` function, which means that you have to handle your code with Promises (either by using `await` or `.then( ).catch( )` – sohaieb azaiez May 17 '23 at 18:34
  • 1
    you can also check this [await is only valid in async functions and the top level bodies of modules](https://stackoverflow.com/questions/69089471/async-await-syntaxerror-await-is-only-valid-in-async-functions-and-the-top-le) – sohaieb azaiez May 17 '23 at 18:40

0 Answers0