【问题标题】:Unable to create a DB using mongoose even with mongod running即使运行 mongod 也无法使用 mongoose 创建数据库
【发布时间】:2020-05-01 14:40:34
【问题描述】:

我在一个选项卡中运行 mongod,而 mongo shell 在另一个选项卡中运行良好。

这是我的代码,我使用 npm 和 mongodb 3.5.6 在我的项目文件夹中安装了 mongoose 版本 5.9.10

    const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27071/fruitsDB", { useUnifiedTopology: true,useNewUrlParser: true, useFindAndModify:false, useCreateIndex:true });

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

const Fruit = mongoose.model("Fruit", fruitSchema);
const fruit = new Fruit ({
    name: "Apple",
    rating: 8,
    review: "gud"
});

fruit.save();

运行,得到一堆错误和警告,根据 mongo shell 没有创建 db

这是我在 hyper 中执行 node app.js 时遇到的错误

UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27071
at new MongooseServerSelectionError (C:\Users\ahuja\Desktop\code\Web Development\fruitsProj\node_modules\mongoose\lib\error\serverSelection.js:22:11)      
at NativeConnection.Connection.openUri (C:\Users\ahuja\Desktop\code\Web Development\fruitsProj\node_modules\mongoose\lib\connection.js:823:32)
at Mongoose.connect (C:\Users\ahuja\Desktop\code\Web Development\fruitsProj\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:\Users\ahuja\Desktop\code\Web Development\fruitsProj\app.js:3:10)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
(node:7980) 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)

【问题讨论】:

  • 这是 UnhandledPromiseRejectionWarning 因此您需要做的就是检查 try catch 块中的连接代码以处理发生的任何错误。请试试这个。
  • @Nayan 我对这个东西很陌生,如果你能指导我如何做这将是一个很大的帮助

标签: node.js mongodb mongoose


【解决方案1】:
mongoose
  .connect("mongodb://localhost/fruitsDB", { useUnifiedTopology: true,useNewUrlParser: true, useFindAndModify:false, useCreateIndex:true })
  .then(() => {
    log.info("successfully connected to db");
  })
  .catch((err) => {
    log.error("db connection failed " + err);
  });

【讨论】:

  • 返回给我这个,即使我在端口 27071 上运行 mongod,mongo shell 也在工作“db connection failed MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27071”
  • 你能查一下this
  • 我在 youtube 上找到了一个解决方案,基本上我只需要在链接中写 localhost 而不是 localhost:27071
猜你喜欢
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 2013-06-28
  • 2018-07-25
  • 2011-06-02
  • 2015-12-30
  • 2016-12-21
相关资源
最近更新 更多