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

时间:2018-02-27 22:33:28

标签: javascript node.js azure botframework azure-cosmosdb

有人可以看看这段代码并告诉我为什么我收到以下错误消息。我已经看过每一个方向,无法理解它为什么或在哪里被打破。

下面的代码显示了docDbClient的硬值,但我还使用了" process.env.Document ..."系统变量无效。这主要取自在线发现的Node Botbuilder样本。它应该连接到CosmosDb数据库。这应该只是加电。使用Bot框架模拟器,在服务器运行的命令提示符处生成错误消息。通过发布的网页尝试,它只是没有错误信息。

提前谢谢!

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

1 个答案:

答案 0 :(得分:2)

您似乎在混合使用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表存储用于bot状态(这是常规表存储,如存储帐户,而不是Cosmos DB Table API):

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

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

价: