when execute code Error occur :Error: (node:1200) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. Error: Userna

23 views
Skip to first unread message

Susheel Kumar

unread,
May 27, 2020, 3:13:00 PM5/27/20
to nodejs



const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);

const errorController = require('./controllers/error');
const User = require('./models/user');

const app = express();
const  MONGODB_URI = 
  'mongodb+srv://susheelkumar:yao@9944@node-projects-xdyvq.mongodb.net/shop'

mongoose.connect(MONGODB_URI, { useNewUrlParser: trueuseUnifiedTopology: true})
const store = new MongoDBStore({
  uri: MONGODB_URI,
  collection'sessions'
});

app.set('view engine''ejs');
app.set('views''views');

const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');
const authRoutes = require('./routes/auth');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname'public')));
app.use(
  session({
    secret'my secret',
    resave: false,
    saveUninitialized: false,
    store: store
  })
);


app.use((req, res, next=> {
  if (!req.session.user) {
    return next();
  }
  User.findById(req.session.user._id)
    .then(user => {
      req.user = user;
      next();
    })
    .catch(err => console.log(err));
});

app.use('/admin', adminRoutes);
app.use(shopRoutes);
app.use(authRoutes);

app.use(errorController.get404);

mongoose
  .connect(MONGODB_URI)
  .then(result => {
    User.findOne().then(user => {
      if (!user) {
        const user = new User({
          name'susheel',
          email'sushe...@gmail.com',
          cart: {
            items: []
          }
        });
        user.save();
      }
    });
    app.listen(3000);
  })
  .catch(err => {
    console.log(err);
  });

Reply all
Reply to author
Forward
0 new messages