javascript - 无法使用 Node 从 Azure Bot 连接到 CosmosDb

标签 javascript node.js azure botframework azure-cosmosdb

有人可以看一下这段代码并告诉我为什么收到以下错误消息吗?我已经从各个方面研究过这个问题,但无法理解它为什么或在哪里出现问题。

下面的代码显示了 docDbClient 的硬值,但我也使用了“process.env.Document...”系统变量,但没有效果。这主要取自在线找到的 Node Botbuilder 示例。它应该连接到 CosmosDb 数据库。这应该刚刚通电。使用 Bot Framework Emulator,在运行服务器的命令提示符处生成错误消息。通过发布的网页尝试,它只是中断,没有错误消息。

提前谢谢您!

var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");
var builder_cognitiveservices = require('botbuilder-cognitiveservices');

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
    console.log('%s listening to %s', server.name, server.url);
});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    openIdMetadata: process.env.BotOpenIdMetadata
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

var docDbClient = new botbuilder_azure.DocumentDbClient({
    host: 'https://xxxxx.table.cosmosdb.azure.com:443',
    masterKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
    database: 'TablesDB',
    collection: 'botdata'
});

var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, docDbClient);

// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, function(session) {
    session.send('You said: %s', session.message.text);
    session.endDialog();
}).set('storage', tableStorage); // Register in Azure Storage

错误:

Error: Failed to initialize azure table client. Error: Error: Error Code:
400 Error Body: {"odata.error":{"code":"BadRequest","message":{"lang":"en-
us","value":"One of the input values is invalid.\r\nActivityId: 676a8f3c-
f287-490c-9062-021cb29ff78a, documentdb-dotnet-sdk/1.20.0 Host/64-bit 
MicrosoftWindowsNT/6.2.9200.0\nRequestID:676a8f3c-f287-490c-9062-
021cb29ff78a\n"}}}

at C:\...\Coffee-Bot\node_modules\botbuilder-azure\lib\AzureBotStorage.js:177:32
at C:\...\Coffee-Bot\node_modules\botbuilder-azure\lib\DocumentDbClient.js:15:17
at C:\...\Coffee-Bot\node_modules\botbuilder-azure\lib\DocumentDbClient.js:76:17
at C:\...\Coffee-Bot\node_modules\documentdb\lib\queryIterator.js:141:28
at C:\...\Coffee-Bot\node_modules\documentdb\lib\queryExecutionContext\proxyQueryExecutionContext.js:71:32
at C:\...\Coffee-Bot\node_modules\documentdb\lib\queryExecutionContext\defaultQueryExecutionContext.js:62:17
at C:\...\Coffee-Bot\node_modules\documentdb\lib\queryExecutionContext\defaultQueryExecutionContext.js:81:32
at C:\...\Coffee-Bot\node_modules\documentdb\lib\queryExecutionContext\defaultQueryExecutionContext.js:136:28
at successCallback (C:\...\Coffee-Bot\node_modules\documentdb\lib\documentclient.js:2360:33)
at C:\...\Coffee-Bot\node_modules\documentdb\lib\documentclient.js:2410:25

最佳答案

您似乎混合了 Cosmos DB Table 端点和 DocumentDB 客户端实例,这解释了 400 Bad Request

对于 DocumentDB API(注意 host.documents. 而不是 .table.):

var docDbClient = new botbuilder_azure.DocumentDbClient({
    host: 'https://xxxxx.documents.cosmosdb.azure.com:443',
    masterKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
    database: 'botdocs',
    collection: 'botdata'
});

var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, docDbClient);

使用 Azure 表存储来存储机器人状态(这是常规表存储,如存储帐户中所示,而不是 Cosmos DB 表 API):

var azureTableClient = new azure.AzureTableClient(tableName, storageName, storageKey);
var tableStorage = new azure.AzureBotStorage({gzipData: false}, azureTableClient);

理论上,if you pass a Cosmos DB Table endpoint对于 azure.AzureTableClient(),您可以使用 Cosmos 作为表存储,表 API 在 Azure 存储和 Cosmos 之间兼容。我没有看到比标准 DocumentDB 类型有任何直接的好处。

引用:

关于javascript - 无法使用 Node 从 Azure Bot 连接到 CosmosDb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49019055/

相关文章:

azure - 是否可以通过脚本或其他方式在 Azure App Insight 中自动创建警报?

php - 将表单发布到更多 URL

javascript - React Native 可重用组件的设置状态问题

node.js - 如何在 node.js 中同时使用多个代理(代理)

Node.js、Express、Mongoose、MongoHQ(保存数据)

azure - 无法在 Outlook 加载项应用程序中获取 sso token

javascript - url 验证正则表达式将电子邮件地址识别为 url

javascript - 文本淡入淡出,jQuery 出现断断续续,相互取代

javascript - NodeJS如何通过输入特定值来获取数据

azure - 更改磁盘类型 Azure ML