MongoClient:尝试使用 Mongoose 时未连接错误

2024-05-21

作为课程的一部分,我正在学习 MongoDB,现在正在学习 Mongoose。我已经完全按照课程中的方式编写了代码,但是当尝试使用node app.js,我收到以下错误:

node app.js

Output:

(node:25772) ` **UnhandledPromiseRejectionWarning: MongoNotConnectedError: MongoClient must be connected to perform this operation**
    `at Object.getTopology (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\node_modules\mongodb\lib\utils.js:391:11)
    at Collection.insertOne (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\node_modules\mongodb\lib\collection.js:150:61)
    at NativeCollection.<computed> [as insertOne] (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:200:33)
    at NativeCollection.Collection.doQueue (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\lib\collection.js:135:23)
    at C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\lib\collection.js:82:24
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:25772) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:25772) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I have mongosh在终端的单独选项卡中运行,并且我尝试了多次搜索来查找类似的问题。我该如何修复它?

这是我的app.js完整代码:

const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/fruitsDB");

const fruitSchema = new mongoose.Schema({
  name: String,
  rating: Number,
  review: String
});

const Fruit = mongoose.model("Fruit", fruitSchema);

const fruit = new Fruit({
  name: "Apple",
  rating: 7,
  review: "Pretty solid as a fruit"
});

fruit.save();
mongoose.connection.close();


const findDocuments = function(db, callback) {

  const collection = db.collection('fruits');

  collection.find({}).toArray(function(err, fruits) {
    assert.equal(err, null);
    console.log("Found the following records");
    console.log(fruits)
    callback(fruits);

  });
}

我遇到了同样的问题,罪魁祸首正在打电话mongoose.connection.close();在 JavaScript 代码主体中。作为AnanthDev 提到 https://stackoverflow.com/questions/69525092/mongoclient-not-connected-error-while-trying-to-use-mongoose/69525139#69525139,mongoose 是异步的,最终会在执行操作之前关闭连接。

以下工作是因为mongoose.connection.close()位于回调函数中,直到执行操作后才会运行。

Fruit.insertMany([kiwi, orange, banana], function(err) {
    if (err) {
        console.log(err);
    } else {
        mongoose.connection.close();
        console.log("Successfully saved all the fruits to fruitsDB");
    }
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MongoClient:尝试使用 Mongoose 时未连接错误 的相关文章

随机推荐