(节点:8592)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:3):MongoParseError:无效的连接字符串

时间:2018-11-16 09:04:41

标签: javascript node.js mongoose

我的代码运行良好,但是今天我运行它时发生了这种情况:

(node:8592) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string

这是app.js的代码段:

var express               = require("express"),
app                   = express(),
bodyParser            = require("body-parser"),
mongoose              = require("mongoose"),
passport              = require("passport"),
localStrategy         = require("passport-local"),
methodOverride        = require("method-override"),
flash                 = require("connect-flash"),
session               = require("express-session"),
campground            = require("./models/campground"),
comment               = require("./models/comments"),  //name of the model is comment
User                  = require("./models/user"),
seedDB                = require("./seeds");

var campgroundRoutes = require("./routers/campground"),
commentRoutes    = require("./routers/comment"),
authRoutes       = require("./routers/auth");

var url = process.env.DATABASE_URL || "mongodb://localhost:27017/yelp_camp"; 
mongoose.connect(url, { useNewUrlParser: true });
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);

我的代码中缺少什么?

1 个答案:

答案 0 :(得分:1)

处理返回的诺言始终是一个好习惯。

.
├── app-d3c9aea5a76e300e113c07b3717683b3.min.css
├── app-f1b7efa7214d328d11623c0f4b3efb19.min.js
├── css
│   ├── custom.css
│   └── style.css
├── index-dev.html
├── index.html
├── js
│   ├── app.js
│   ├── custom.js
│   └── script.js
├── META-INF
│   └── context.xml
└── WEB-INF
    ├── classes
    │   ├── com
    │   │   └── darshan
    │   │       └── SourceMapFilter.class
    │   ├── readme.txt
    │   └── static-bundles.json
    └── web.xml

因此,这将避免使用var url = process.env.DATABASE_URL || "mongodb://localhost:27017/yelp_camp"; mongoose.connect(url, { useNewUrlParser: true }) .then(() => console.log("Connection Successful")) .catch(err => console.log(err)); ,并且您还将知道连接失败的原因。很有可能是URL问题。