议程连接?"/>
已建立连接后如何进行议程连接?
我有一个文件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 连接 避免冲突和连接超时问题
更多推荐
已建立连接后如何进行议程连接?
发布评论