gpt4 book ai didi

javascript - ( Node :8592) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string

转载 作者:行者123 更新时间:2023-11-30 11:11:54 34 4
gpt4 key购买 nike

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

(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);

我的代码中缺少什么?

最佳答案

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

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));

因此这将避免 UnhandledPromiseRejectionWarning 并且您将知道连接失败的原因。很可能是 URL 的问题。

关于javascript - ( Node :8592) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53334533/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com