已建立连接后如何进行议程连接?

编程入门 行业动态 更新时间:2024-09-26 02:22:18

已建立连接后如何进行<a href=https://www.elefans.com/category/jswz/34/1771149.html style=议程连接?"/>

已建立连接后如何进行议程连接?

我有一个文件db.js,它有一个函数MongoDBConnection,该函数的工作连接到我的MongoDB图集,但在我的项目中,我还使用agenda包,它需要建立一个MongoDB图集连接,所以现在我的问题是我已经在我的 index.js 文件中调用 MongoDBConnection 函数来建立连接,

那么现在我如何连接到 Atlas 以进行议程?我连接议程的方式在生产服务器中出现错误,解决方案是什么?是因为我连接了两次吗?

错误:

ERROR  Unhandled Promise Rejection     {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoNetworkTimeoutError: connection timed out","reason":{"errorType":"MongoNetworkTimeoutError","errorMessage":"connection timed out","connectionGeneration":0,"stack":["MongoNetworkTimeoutError: connection timed out","    at connectionFailureError (/var/task/node_modules/agenda/node_modules/mongodb/lib/cmap/connect.js:389:20)","    at TLSSocket.<anonymous> (/var/task/node_modules/agenda/node_modules/mongodb/lib/cmap/connect.js:310:22)","    at Object.onceWrapper (node:events:627:28)","    at TLSSocket.emit (node:events:513:28)","    at Socket._onTimeout (node:net:570:8)","    at listOnTimeout (node:internal/timers:569:17)","    at process.processTimers (node:internal/timers:512:7)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: MongoNetworkTimeoutError: connection timed out","    at process.<anonymous> (file:///var/runtime/index.mjs:1186:17)","    at process.emit (node:events:525:35)","    at emit (node:internal/process/promises:149:20)","    at processPromiseRejections (node:internal/process/promises:283:27)","    at processTicksAndRejections (node:internal/process/task_queues:96:32)","    at runNextTicks (node:internal/process/task_queues:64:3)","    at listOnTimeout (node:internal/timers:538:9)","    at process.processTimers (node:internal/timers:512:7)"]}

[ERROR] [1690349259284] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 400.

RequestId: 44c829c3-bbad-4d67-9bb6-8951360bd02c Error: Runtime exited with error: exit status 128

Runtime.ExitError

db.js:

import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();

mongoose.set('strictQuery', false);

export const MongoDBConnection = async () => {
    try {
        await mongoose.connect('mymongourl')
        console.log('Connected to database');
    } catch (err) {
        console.log('Error connecting to database: ' + err);
    }
};

我只是像这样在我的index.js文件中调用这个函数

MongoDBConnection()

议程连接:

import Agenda from 'agenda'
const mongoConnectionString = 'mongoaltasurl';
const agenda = new Agenda({ db: { address: mongoConnectionString } });
回答如下:

在db.js中

import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
mongoose.set('strictQuery', false);

export const MongoDBConnection = async () => {
    try {
        await mongoose.connect('mymongourl');
        console.log('Connected to database');
        return mongoose.connection; 
    } catch (err) {
        console.log('Error connecting to database: ' + err);
        throw err;
    }
};

在index.js中

    import { MongoDBConnection } from './db.js';
import Auth from './Auth'; // Assuming you have the Auth.js file

const start = async () => {
    try {
        const mongooseConnection = await MongoDBConnection();
        const agenda = new Agenda({ mongo: mongooseConnection });

        Auth(mongooseConnection);

    } catch (err) {
        console.error('Error initializing the app: ', err);
    }
};

start();

Auth.js

export default function Auth(mongooseConnection) {
    const agenda = new Agenda({ mongo: mongooseConnection });
    //code    
}

您可以检查您的应用程序和 Agenda 包是否使用相同的 MongoDB 连接 避免冲突和连接超时问题

更多推荐

已建立连接后如何进行议程连接?

本文发布于:2024-05-31 07:27:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:议程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!