【问题标题】:Cannot connect to CosmosDb from Azure Bot using Node无法使用 Node 从 Azure Bot 连接到 CosmosDb
【发布时间】:2018-02-28 08:14:52
【问题描述】:

有人可以看看这段代码并告诉我为什么我会收到以下错误消息。我从各个方面都看过这个,但不明白它为什么或在哪里坏了。

下面的代码显示了 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

【问题讨论】:

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


    【解决方案1】:

    您似乎正在混合使用 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 endpointazure.AzureTableClient() 可以使用 Cosmos 作为 Table storage,Table API 在 Azure Storage 和 Cosmos 之间是兼容的。与标准 DocumentDB 类型相比,我没有看到任何直接的好处。

    参考:

    【讨论】:

    • 好的,我想我明白你的意思了(尽管在我尝试解决之后可能会出现问题)。最初,我使用的是表存储,但它只会记录对话元数据。同样,它的设置与示例类似,但根本无法捕捉对话。
    • 我解决了这个特殊问题,但它仍然没有发布。我收到“错误:无法初始化 azure 表客户端。错误:错误:错误代码:ECONNRESET 错误正文:未定义”。为什么body是未定义的?
    • 我应该补充一点,“Body: undefined”错误是在使用与“localhost:3978”连接的模拟器进行测试时。如果我使用实际的主机端点并在线运行,第一个用户帖子会产生 502 机器人超时错误。之后,聊天帖子返回状态 200,但仍未发布到数据库。
    • 不确定您做错了什么,但只需按照此处的示例进行操作 - github.com/MicrosoftDocs/azure-docs/blob/master/articles/…
    猜你喜欢
    • 2018-05-07
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多