node.jsazurebotframeworkazure-language-understandingazure-function-async

How to fix 'Unexpected Token' error with async function


I am developing a web app bot on azure (v3) and I am using async methods but I can't seem to solve an issue which is SyntaxError: Unexpected token function.

I've tried updating my nodeJS from 6.9.4 to 8.9 but that didn't work. I also ran npm i -g azure-functions-core-tools@core but still nothing.

class OAuthHelpers {
/**
 * Enable the user to schedule meeting and send an email attachment via the bot.
 * @param {TurnContext} turnContext 
 * @param {TokenResponse} tokenResponse 
 * @param {*} emailAddress The email address of the recipient
 */

async function createevent(turnContext, tokenResponse, emailAddress) {
    if (!turnContext) {
        throw new Error('OAuthHelpers.createevent(): `turnContext` cannot be undefined.');
    }
    if (!tokenResponse) {
        throw new Error('OAuthHelpers.createevent(): `tokenResponse` cannot be undefined.');
    }


    var client = new SimpleGraphClient(tokenResponse.token);

    // Calls the Graph API with the subject and content message...
    await client.createevent(
        emailAddress,
        `Lunch`,
        `I will be taking everyone to lunch as a reward for your hardwork.`
    );

    // Success message...
    await turnContext.sendActivity(`Success! I have scheduled a meeting with you and ${ emailAddress } have created an event on each of their calendars.`);
    } 

I want the bot to run normally but it can't because azure can't detect the async function for some reason. Any help is appreciated


Solution

  • The OAuthHelpers class requires 'simple-graph-client' which houses all of the methods you are looking to utilize. In the original sample your code draws from, BotBuilder-Sample 24.bot-authentication-msgraph, if you navigate to the simple-graph-client.js file, you will see the methods called (i.e. sendMail, getRecentMail, getMe, and getManager) in the OAuthHelpers.js file.

    If you haven't already, you will need to include a method for creating an event. This, in turn is called from the OAuthHelpers.js file as part of the bot dialog.

    It's hard to know what is what without more code, but my guess is the token is being passed into your createevent method but, as the method (likely) doesn't exist as a graph api call, it doesn't know what to do with it.

    Check out the following links for guidance:

    Hope of help!