带有MongoDB ReferenceError的SvelteKit:未定义全局 [英] SvelteKit With MongoDB ReferenceError: global is not defined

查看:21
本文介绍了带有MongoDB ReferenceError的SvelteKit:未定义全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置MongoDB连接库函数。我知道这个函数很可靠,它在很多地方使用(搜索Global在这里用来维护跨热重新加载的缓存连接),你会发现它有很多用处,包括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";:";Version";:";1.0.0-next.118";,

编辑:在这个问题上花费了大量时间后,全局未定义错误似乎来自import { MongoClient } from "mongodb";,如果我添加适当的console.log,我可以看到MongoClient函数在服务器上运行良好,但随后我在客户机上得到全局错误。服务器根本没有指示任何错误。

api

所以我调用的import { connectToDatabase } from '$lib/database'不是在.js帮助器文件或推荐答案样式(.js)endpoints中。我试图使用该导入并直接从xxx.svelte文件的<script>部分调用数据库。

绝对不行。这会立即生成全局未定义错误。

这篇关于带有MongoDB ReferenceError的SvelteKit:未定义全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆