gpt4 book ai didi

mongodb - SvelteKit 与 MongoDB ReferenceError : global is not defined

转载 作者:行者123 更新时间:2023-12-05 04:45:21 24 4
gpt4 key购买 nike

我正在尝试设置 MongoDB 连接库函数。我知道这个函数很可靠,它用在很多地方(搜索 Global is used here to maintain a cached connection across hot reloads),你会发现很多用途,包括 next .js 版本。请注意,数据库连接的全局存储的目的是减少任何时候使用的数据库连接的总数。

我不明白的是当我通过 import { connectToDatabase } from '$lib/database';

导入这个库时遇到的错误

数据库.js

// https://github.com/mongodb-developer/mongodb-next-todo/blob/main/util/mongodb.js
import { ENV_OBJ } from "$lib/env";
import { MongoClient } from "mongodb";

const uri = ENV_OBJ.MONGODB_URI;

if (!uri) {
throw new Error("Please define the Mongodb uri environment variable inside .env");
}

/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.mongo

if (!cached) {
cached = global.mongo = { conn: null, promise: null }
}

export const connectToDatabase = async() => {
if (cached.conn) {
return cached.conn;
}

if (!cached.promise) {
const options = {
useNewUrlParser: true,
useUnifiedTopology: true
};

cached.promise = MongoClient.connect(MONGODB_URI, opts).then((client) => {
return {
client,
db: client.db(MONGODB_DB),
}
})
}
cached.conn = await cached.promise;
return cached.conn;
}

错误:

global is not defined

ReferenceError: global is not defined
at node_modules/mongodb/lib/promise_provider.js (http://localhost:3000/node_modules/.vite/mongodb.js?v=3885e04e:548:25)
at __require2 (http://localhost:3000/node_modules/.vite/chunk-6ODJH7E3.js?v=3885e04e:10:44)
at node_modules/mongodb/lib/utils.js (http://localhost:3000/node_modules/.vite/mongodb.js?v=3885e04e:6524:30)
at __require2 (http://localhost:3000/node_modules/.vite/chunk-6ODJH7E3.js?v=3885e04e:10:44)
at node_modules/mongodb/lib/cursor/abstract_cursor.js (http://localhost:3000/node_modules/.vite/mongodb.js?v=3885e04e:10873:19)
at __require2 (http://localhost:3000/node_modules/.vite/chunk-6ODJH7E3.js?v=3885e04e:10:44)
at node_modules/mongodb/lib/index.js (http://localhost:3000/node_modules/.vite/mongodb.js?v=3885e04e:25281:29)
at __require2 (http://localhost:3000/node_modules/.vite/chunk-6ODJH7E3.js?v=3885e04e:10:44)
at http://localhost:3000/node_modules/.vite/mongodb.js?v=3885e04e:25616:23

请注意,我确实在我生成的最小 sveltekit 存储库中看到一个名为 global.d.ts 的文件,我不确定它的用途。它仅包含:

 /// <reference types="@sveltejs/kit" /> 

关于导致错误的原因有什么想法吗?

引用:“@sveltejs/kit”:“版本”:“1.0.0-next.118”,

编辑:在这个问题上花了很多时间之后,全局未定义错误似乎来自 import { MongoClient } from "mongodb"; 如果我添加适当的 console.logs,我可以看到 MongoClient 函数在服务器上运行良好,但随后在客户端出现全局错误。服务器指示完全没有错误。

最佳答案

原来我调用import { connectToDatabase } from '$lib/database'不在 .js 帮助文件或 api 样式 (.js) 中 endpoints .我试图使用该导入并直接从 <script> 进行数据库调用xxx.svelte 文件的一部分。

绝对不行。这会立即生成一个global not defined 错误。

关于mongodb - SvelteKit 与 MongoDB ReferenceError : global is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69171641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com