最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何将alexa技能与现有的mongodb地图集集群连接

IT培训 admin 5浏览 0评论

如何将alexa技能与现有的mongodb地图集集群连接

我对Alexa技能还很陌生。我掌握了“ hello world”的技能,并进行了一些HTTP请求。

现在,我正在尝试将我的技能与现有的MongoDB地图集集群联系起来。我的目标是从我的集群中检索一些数据,并将其用于我的技能中,以响应向Alexa提出问题的用户。

[我认为我已经建立了与地图集的连接,但是当我尝试对数据库中的集合进行某些操作时,出现了“ MongoError:拓扑被破坏”错误。

正如我已经读过的那样,问题可能出在AWS Lambda的工作流中。它处于无状态工作状态,当我尝试阅读集合时,连接可能已经断开,但我不知道如何解决此问题。

这是我目前建立连接并只打印文档计数的方法。我测试了如何移动客户端连接,但我总是遇到相同的错误。

当前建立连接的方法


const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<USER>:<PASS>@<CLUSTER>?retryWrites=true";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
    const collection = client.db("database").collection("data");
    console.log(collection.countDocuments());
    client.close();
});

这是我的一种技能意图:


const HelloWorldIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
    },
    handle(handlerInput) {
        const speechText = 'Hello World!';
        return handlerInput.responseBuilder
            .speak(speechText)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
}};

这是实际错误的样子:


    Promise {
    <rejected> { MongoError: Topology was destroyed
    at initializeCursor (/var/task/node_modules/mongodb- 
   core/lib/cursor.js:603:25)
    at nextFunction (/var/task/node_modules/mongodb-core/lib/cursor.js:463:12)
    at AggregationCursor.Cursor.next (/var/task/node_modules/mongodb-core/lib/cursor.js:763:3)
    at AggregationCursor.Cursor._next (/var/task/node_modules/mongodb/lib/cursor.js:211:36)
    at fetchDocs (/var/task/node_modules/mongodb/lib/operations/cursor_ops.js:217:12)
    at toArray (/var/task/node_modules/mongodb/lib/operations/cursor_ops.js:247:3)
    at executeOperation (/var/task/node_modules/mongodb/lib/utils.js:420:24)
    at AggregationCursor.Cursor.toArray (/var/task/node_modules/mongodb/lib/cursor.js:824:10)
    at countDocuments (/var/task/node_modules/mongodb/lib/operations/collection_ops.js:233:37)
    at /var/task/node_modules/mongodb/lib/utils.js:437:24 name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} } }
回答如下:

我在用于连接MongoDb的其他AWS lambda函数上也遇到了类似的问题,我发现我们需要提供与Mongodb连接的其他参数,如下所述。

var options = { server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } } }; mongoose.connect(secrets.db, options);

[请尝试让我知道是否有帮助。

如何将alexa技能与现有的mongodb地图集集群连接

我对Alexa技能还很陌生。我掌握了“ hello world”的技能,并进行了一些HTTP请求。

现在,我正在尝试将我的技能与现有的MongoDB地图集集群联系起来。我的目标是从我的集群中检索一些数据,并将其用于我的技能中,以响应向Alexa提出问题的用户。

[我认为我已经建立了与地图集的连接,但是当我尝试对数据库中的集合进行某些操作时,出现了“ MongoError:拓扑被破坏”错误。

正如我已经读过的那样,问题可能出在AWS Lambda的工作流中。它处于无状态工作状态,当我尝试阅读集合时,连接可能已经断开,但我不知道如何解决此问题。

这是我目前建立连接并只打印文档计数的方法。我测试了如何移动客户端连接,但我总是遇到相同的错误。

当前建立连接的方法


const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<USER>:<PASS>@<CLUSTER>?retryWrites=true";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
    const collection = client.db("database").collection("data");
    console.log(collection.countDocuments());
    client.close();
});

这是我的一种技能意图:


const HelloWorldIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
    },
    handle(handlerInput) {
        const speechText = 'Hello World!';
        return handlerInput.responseBuilder
            .speak(speechText)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
}};

这是实际错误的样子:


    Promise {
    <rejected> { MongoError: Topology was destroyed
    at initializeCursor (/var/task/node_modules/mongodb- 
   core/lib/cursor.js:603:25)
    at nextFunction (/var/task/node_modules/mongodb-core/lib/cursor.js:463:12)
    at AggregationCursor.Cursor.next (/var/task/node_modules/mongodb-core/lib/cursor.js:763:3)
    at AggregationCursor.Cursor._next (/var/task/node_modules/mongodb/lib/cursor.js:211:36)
    at fetchDocs (/var/task/node_modules/mongodb/lib/operations/cursor_ops.js:217:12)
    at toArray (/var/task/node_modules/mongodb/lib/operations/cursor_ops.js:247:3)
    at executeOperation (/var/task/node_modules/mongodb/lib/utils.js:420:24)
    at AggregationCursor.Cursor.toArray (/var/task/node_modules/mongodb/lib/cursor.js:824:10)
    at countDocuments (/var/task/node_modules/mongodb/lib/operations/collection_ops.js:233:37)
    at /var/task/node_modules/mongodb/lib/utils.js:437:24 name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} } }
回答如下:

我在用于连接MongoDb的其他AWS lambda函数上也遇到了类似的问题,我发现我们需要提供与Mongodb连接的其他参数,如下所述。

var options = { server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } } }; mongoose.connect(secrets.db, options);

[请尝试让我知道是否有帮助。

发布评论

评论列表 (0)

  1. 暂无评论