【问题标题】:How to fix 'Unexpected Token' error with async function如何使用异步功能修复“意外令牌”错误
【发布时间】:2019-01-23 23:57:03
【问题描述】:

我正在 azure (v3) 上开发一个 Web 应用程序机器人,并且我正在使用异步方法,但我似乎无法解决 SyntaxError: Unexpected token function 的问题。

我尝试将我的 nodeJS 从 6.9.4 更新到 8.9,但没有成功。我还运行了 npm i -g azure-functions-core-tools@core 但仍然没有。

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.`);
    } 

我希望机器人正常运行,但它不能,因为 azure 由于某种原因无法检测到异步功能。任何帮助表示赞赏

【问题讨论】:

  • 你是否成功安装了node 8.x版本?还是升级失败了?如果是这样,请考虑使用 nvm 节点版本管理器,它允许您以最简单的方式在版本之间切换。
  • 我进入我的应用程序设置并将其从 8.9 更改为运行 node -v 时在终端中显示 8.9
  • 你能显示更多部分代码吗,我不确定上面的行是否真的有问题
  • 我添加了更多代码以便您更好地理解
  • 您提到您使用的是 v3 SDK,但“turnContext”是 v4 SDK 对象。 v3 使用“会话”。您能否说明您使用的是哪个版本的 SDK?

标签: node.js azure botframework azure-language-understanding azure-function-async


【解决方案1】:

OAuthHelpers 类需要“simple-graph-client”,其中包含您希望使用的所有方法。在您的代码从中提取的原始示例 BotBuilder-Sample 24.bot-authentication-msgraph 中,如果您导航到 simple-graph-client.js 文件,您将在 OAuthHelpers 中看到调用的方法(即 sendMail、getRecentMail、getMe 和 getManager) .js 文件。

如果您还没有,您将需要包含用于创建事件的方法。这反过来又从 OAuthHelpers.js 文件中调用,作为机器人对话框的一部分。

如果没有更多代码,很难知道什么是什么,但我的猜测是令牌被传递到您的 createevent 方法中,但是由于该方法(可能)不作为图形 api 调用存在,所以它不知道如何处理它。

查看以下链接以获得指导:

  • MS Graph sample 显示前 3 个日历事件的 GET 调用
  • MS Graph 单元测试example,但演示了一个事件 POST
  • API reference 用于创建事件
  • 添加info 以创建重复事件...可能会很有用

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2023-03-07
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多