操作不正确的状态:“新的”允许状态:“ [运行,启动]”

发布于 2025-02-10 22:50:09 字数 946 浏览 1 评论 0 原文

我正在测试只有一个测试案例。我验证了mongodb方法,它们似乎要达到 日期。 Github也没有开放问题。

Error:    at MongoMemoryServer.getUri (node_modules/mongodb-memory-server- 
          core/src/MongoMemoryServer.ts:706:15)
          at src/test/setup.ts:7:32
          at src/test/setup.ts:8:71
          at Object.<anonymous>.__awaiter (src/test/setup.ts:4:12)
          at Object.<anonymous> (src/test/setup.ts:5:22)   



import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";

let mongo: any;
beforeAll(async () => {
  mongo = new MongoMemoryServer();
  const mongoUri = await mongo.getUri();
  await mongoose.connect(mongoUri);
});

beforeEach(async () => {
  const collections = await mongoose.connection.db.collections();
  for (let collection of collections) {
    await collection.deleteMany({});
  }
});

afterAll(async () => {
  await mongo.stop();
  await mongoose.connection.close();
});

There is just one test case that I'm testing. I verified the MongoDB methods and they seem to be up to
date. No open issues on GitHub as well.

Error:    at MongoMemoryServer.getUri (node_modules/mongodb-memory-server- 
          core/src/MongoMemoryServer.ts:706:15)
          at src/test/setup.ts:7:32
          at src/test/setup.ts:8:71
          at Object.<anonymous>.__awaiter (src/test/setup.ts:4:12)
          at Object.<anonymous> (src/test/setup.ts:5:22)   



import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";

let mongo: any;
beforeAll(async () => {
  mongo = new MongoMemoryServer();
  const mongoUri = await mongo.getUri();
  await mongoose.connect(mongoUri);
});

beforeEach(async () => {
  const collections = await mongoose.connection.db.collections();
  for (let collection of collections) {
    await collection.deleteMany({});
  }
});

afterAll(async () => {
  await mongo.stop();
  await mongoose.connection.close();
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝不服输 2025-02-17 22:50:09

频道行 mongo = new Mongomemoryserver(); to mongo =等待mongomemoryserver.create()

channge the line mongo = new MongoMemoryServer(); to mongo = await MongoMemoryServer.create()

横笛休吹塞上声 2025-02-17 22:50:09

我遇到了同样的问题,然后查看我想知道您首先必须使用 new 创建对象,然后使用start()启动实例:

mongod = new MongoMemoryServer()
await mongod.start()

const uri = await mongod.getUri()

const mongooseOptions = {
    useNewUrlParser:true,
};

await mongoose.connect(uri,mongooseOptions)

I had your same issue and by looking at the documentation I figure out that you first have to create the object with new and then start the instance with start():

mongod = new MongoMemoryServer()
await mongod.start()

const uri = await mongod.getUri()

const mongooseOptions = {
    useNewUrlParser:true,
};

await mongoose.connect(uri,mongooseOptions)
听闻余生 2025-02-17 22:50:09

您有2个解决此问题的选项:

1-更改行:

mongo = new MongoMemoryServer(); 

TO:

mongo = await MongoMemoryServer.create();

2-更改行:

 mongo = new MongoMemoryServer();
 const mongoUri = await mongo.getUri();
 await mongoose.connect(mongoUri);

to::

mongod = new MongoMemoryServer()
await mongod.start()

const uri = await mongod.getUri()

const mongooseOptions = {
useNewUrlParser:true,
};

await mongoose.connect(uri, mongooseOptions)

You have 2 options to solve this problem:

1- Change the line:

mongo = new MongoMemoryServer(); 

to:

mongo = await MongoMemoryServer.create();

2- Change the lines:

 mongo = new MongoMemoryServer();
 const mongoUri = await mongo.getUri();
 await mongoose.connect(mongoUri);

To:

mongod = new MongoMemoryServer()
await mongod.start()

const uri = await mongod.getUri()

const mongooseOptions = {
useNewUrlParser:true,
};

await mongoose.connect(uri, mongooseOptions)
家住魔仙堡 2025-02-17 22:50:09
mongod = new MongoMemoryServer();
await mongod.start();
const mongoUri = await mongod.getUri();
await mongoose.connect(mongoUri);
mongod = new MongoMemoryServer();
await mongod.start();
const mongoUri = await mongod.getUri();
await mongoose.connect(mongoUri);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文