mongoose 我遇到connect-mongo连接错误

ecbunoof  于 2023-03-12  发布在  Go
关注(0)|答案(1)|浏览(95)

我正在尝试使用connect-mongo,但出现以下错误
此.选项=选项??{};语法错误:意外标记?我的代码是

//database connection
const mongoose = require('mongoose');
mongoose.connect(process.env.DATABASE_URL,{ useNewUrlParser: true, useUnifiedTopology: true } ,function(err) {
    if (err) {
        console.log("Database Not Connected", err);
    } else {
        console.log("Database Connected")
    }
});
mongoose.set('useFindAndModify', false);

//body parser configutation
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));

//Other usage for application
app.use(express.static("public"));
app.set('view engine','ejs');
// const cookieParser = require('cookie-parser')
// app.use(cookieParser(process.env.SESSION_SECRET));
const MongoStore = require("connect-mongo");
app.use(require('express-session')({
    secret: process.env.SESSION_SECRET,
    // cookie : { maxAge: 24 * 60 * 60 * 1000, secure: true,httpOnly : false },
    resave: true,
    saveUninitialized: true,
    store: MongoStore.create({mongoUrl: process.env.DATABASE_URL, collectionName: "sessions",})
}));



//passport configuration
const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());

const localStrategy = require('passport-local');
const User = require('./models/user');
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

//caching configuration
app.use((req,res,next) => {
    res.set('Cache-Control','no-cache, private, no-store, must revalidate, max-stale=0, post-check=0, pre-check=0')
    res.locals.currentUser = req.user;
    next();
});

//morgan configuration
const morgan = require('morgan');
app.use(morgan('dev'));

//Home page
app.get('/',(req,res)=>{res.render('home');})

//Server Start
https.createServer({
    key: fs.readFileSync("key.pem"),
    cert: fs.readFileSync("cert.pem"),
  },app).listen(port, ()=>{console.log(`server is runing at port ${port}`)});

我尝试过更改版本,但不起作用....我需要将connect-mongo正确连接到应用程序。我该如何操作

9jyewag0

9jyewag01#

请试试这个

mongoose.connect("enter_your_database_url", {
    useNewUrlParser: true,
    useUnifiedTopology: true
},
function(err){
    if(err){
        console.log("Connection to Database Failed !!!!");
        console.log(err);
    } else{
       console.log("Connected to MongoDB");
    }
});

相关问题