最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sveltekit 在部署到 Vercel 时失去与 MongoDB 的连接

网站源码admin44浏览0评论

Sveltekit 在部署到 Vercel 时失去与 MongoDB 的连接

Sveltekit 在部署到 Vercel 时失去与 MongoDB 的连接

我最近在 svelte.config.js 中使用

@sveltejs/adapter-vercel
适配器向 Vercel 部署了一个小型 SvelteKit 网络应用程序,如下所示:

import adapter from '@sveltejs/adapter-vercel';

const config = {
    kit: {
        adapter: adapter(),
    },
};

作为数据库,我使用 MongoDB(托管在 Atlas 免费集群上)。问题是有时(看似随机)无法再访问数据库并抛出以下错误:

MongoTopologyClosedError: Topology is closed
    at processWaitQueue (/var/task/node_modules/mongodb/lib/sdam/topology.js:513:46)
    at Topology.selectServer (/var/task/node_modules/mongodb/lib/sdam/topology.js:282:9)
    at Topology.<anonymous> (/var/task/node_modules/mongodb/lib/sdam/topology.js:41:94)
    at node:internal/util:375:7
    at new Promise (<anonymous>)
    at Topology.selectServerAsync (node:internal/util:361:12)
    at executeOperationAsync (/var/task/node_modules/mongodb/lib/operations/execute_operation.js:74:35)
    at /var/task/node_modules/mongodb/lib/operations/execute_operation.js:12:45
    at maybeCallback (/var/task/node_modules/mongodb/lib/utils.js:263:21)
    at executeOperation (/var/task/node_modules/mongodb/lib/operations/execute_operation.js:12:38)

在做了一些研究之后,我不清楚到底是什么导致了这个错误。这就是我连接到数据库的方式:

import { MongoClient } from 'mongodb';

const client = new MongoClient(PRIVATE_DB_CONNECTION_STRING);
const db = client.db();

export async function connectToDatabase() {
    try {
        await client.connect();
        console.log('Connected to mongodb');
    } catch (error) {
        console.log(error);
    }
}

export const myCollection = db.collection('randomCollection');

connectToDatabase()
方法在应用程序启动时在
hooks.server.ts
中调用。 有谁知道什么可能导致这个错误以及如何摆脱它?

回答如下:
发布评论

评论列表(0)

  1. 暂无评论