我正在尝试使用 mongodb 所以我安装了 mongoose 包
但问题是当我这样写的时候
const express = require("express");
const dotenv = require("dotenv");
const mongoose = require("mongoose"); //getting error here
Run Code Online (Sandbox Code Playgroud)
它向我显示这样的错误
const utf8Encoder = new TextEncoder();
^
ReferenceError: TextEncoder is not defined
Run Code Online (Sandbox Code Playgroud)
如果我评论 mongoose 行,我不会收到任何错误,但即使我检查了我的节点版本为 16.5.0,我也需要使用它,我尝试查找发生相同错误的旧帖子,但它对我来说无法理解,有什么帮助吗?旧帖子链接 在此处输入链接描述
如何修复此错误
问题似乎出在 mongoose 和 mongodb 包上,因为它在以下情况下工作正常
mongoose.connect('mongodb+srv://mydb:<password>@cluster0.w1opr.mongodb.net/test?retryWrites=true&w=majority');
Run Code Online (Sandbox Code Playgroud)
被删除它在 repl.it 云环境上也可以正常工作这是我的代码
var express = require('express');
var ejs = require('ejs');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb+srv://mydb:<password>@cluster0.w1opr.mongodb.net/test?
retryWrites=true&w=majority');
app.set('view engine','ejs')
app.use(bodyParser.urlencoded({extended: true}));
.
.
.
app.listen(3000,function(){
console.log('Server is running on port 3000');
});
Run Code Online (Sandbox Code Playgroud) 我有这两个文件:
测试.js:
const jsdom = require("jsdom");
const hello = () => {
console.log("hello!");
};
module.exports = {
hello
};
Run Code Online (Sandbox Code Playgroud)
和
服务器.js:
const hello = require('./stuff/test');
hello.hello();
Run Code Online (Sandbox Code Playgroud)
目录结构:
myprojectfolder
backend
src
stuff
test.js
server.js
Run Code Online (Sandbox Code Playgroud)
当我运行时,server.js
我收到ReferenceError: TextEncoder is not defined
错误:
/home/myusername/projects/myprojectfolder/node_modules/whatwg-url/lib/encoding.js:2
const utf8Encoder = new TextEncoder();
^
ReferenceError: TextEncoder is not defined
Run Code Online (Sandbox Code Playgroud)
如果我const jsdom = require("jsdom");
从 中删除行test.js
,server.js
则运行正常并且没有任何错误(输出hello
)。
为什么会发生这种情况以及如何修复它(同时仍然能够jsdom
在内部导入和使用test.js
?)。
我在尝试学习如何测试 next.js 应用程序时收到此错误消息我尝试从配置顶部的 utils 导入文本编码器以及测试文件,但没有成功
ReferenceError: TextEncoder is not defined
at Object.<anonymous> (node_modules/whatwg-url/lib/encoding.js:2:21)
at Object.<anonymous> (node_modules/whatwg-url/lib/url-state-machine.js:5:34)
at Object.<anonymous> (node_modules/whatwg-url/lib/URL-impl.js:2:13)
at Object.<anonymous> (node_modules/whatwg-url/lib/URL.js:442:14)
at Object.<anonymous> (node_modules/whatwg-url/webidl2js-wrapper.js:3:13)
at Object.<anonymous> (node_modules/whatwg-url/index.js:3:34)
at Object.<anonymous> (node_modules/mongodb-connection-string-url/src/index.ts:1:1)
at Object.<anonymous> (node_modules/mongodb/src/connection_string.ts:3:1)
at Object.<anonymous> (node_modules/mongodb/src/mongo_client.ts:11:1)
at Object.<anonymous> (node_modules/mongodb/src/change_stream.ts:17:1)
at Object.<anonymous> (node_modules/mongodb/src/index.ts:3:1)
at Object.<anonymous> (node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js:8:16)
at Object.<anonymous> (node_modules/mongoose/lib/drivers/node-mongodb-native/index.js:7:18)
at Object.<anonymous> (node_modules/mongoose/lib/index.js:7:25)
at Object.<anonymous> (node_modules/mongoose/index.js:8:18)
at Object.<anonymous> (library/mongoDB.js:15:56)
at Object.<anonymous> (library/players.js:15:18)
at Object.<anonymous> (pages/index.js:18:18)
at Object.<anonymous> (tests/index.test.js:6:53)
Run Code Online (Sandbox Code Playgroud)
玩笑配置文件
const nextJest = require('next/jest')
const createJestConfig = nextJest({
dir: './',
})
const …
Run Code Online (Sandbox Code Playgroud) node.js ×3
javascript ×2
jestjs ×1
jsdom ×1
mongodb ×1
mongoose ×1
next.js ×1
nodes ×1
npm-package ×1