TypeError:azure.DocumentDbClient不是构造函数

时间:2019-11-25 00:57:31

标签: botframework

我正在构建一个机器人,以使用具有以下依赖项的Node SDK连接到Azure Cosmos DB:

"dependencies": {
    "botbuilder": "~4.6.2",
    "botbuilder-azure": "^4.6.2",
},

这是我从this official tutorial复制的代码。该教程适用于SDK v3,很遗憾,此配置没有适用于v4的官方教程。

var azure = require('botbuilder-azure');

var documentDbOptions = {
    host: <secret>, 
    masterKey: <secret>, 
    database: 'database',   
    collection: 'collection'
};

var docDbClient = new azure.DocumentDbClient(documentDbOptions);
var cosmosStorage = new azure.AzureBotStorage({ gzipData: false }, docDbClient);

这是完整的异常堆栈:

evandro@mypc:~/Projects/pluralsight-bot$ npm start

> pluralsight-bot@1.0.0 start /home/evandro/Projects/pluralsight-bot
> node ./index.js

/home/evandro/Projects/pluralsight-bot/index.js:28
var docDbClient = new azure.DocumentDbClient(documentDbOptions);
                  ^

TypeError: azure.DocumentDbClient is not a constructor
    at Object.<anonymous> (/home/evandro/Projects/pluralsight-bot/index.js:28:19)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11

1 个答案:

答案 0 :(得分:1)

您链接的教程的日期为12/12/2017。用Bot Framework的术语来说,非常是过时的,尤其是因为它是针对v3而不是v4的。在顶部,它具有到v4的链接,尽管它仅带您到文档的v4,而没有本文。 What is the equivalent of the -IgnoreChanges switch for entity-framework core in CLI?

这是相关代码:

const { CosmosDbPartitionedStorage } = require("botbuilder-azure");

[...]

// initialized to access values in .env file.
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });

// Create local Memory Storage - commented out.
// var storage = new MemoryStorage();

// Create access to CosmosDb Storage - this replaces local Memory Storage.
var storage = new CosmosDbPartitionedStorage({
    cosmosDbEndpoint: process.env.DB_SERVICE_ENDPOINT, 
    authKey: process.env.AUTH_KEY, 
    databaseId: process.env.DATABASE_ID,
    containerId: process.env.CONTAINER
})

注意:如果您使用的是未分区的现有数据库,则需要使用CosmosDbStorage而不是CosmosDbPartitionedStorage。此外,文档中的示例错误地导入了CosmosDbStorage而不是CosmosDbPartitionedStorage。我已经提交了PR来解决此问题。