Method of depreciated Mongoosis

Asked

Viewed 135 times

0

I’m trying to make a connection with mblab but the connection method is deprecated. Version of the method is 4.9.7. What I need is 4.13.7.

Structure:

inserir a descrição da imagem aqui

server.js

const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const hbs = require('hbs');
const expressHbs = require('express-handlebars');
const config = require('./config/secret');

const app = express();

mongoose.connect(config.database, function(err) {
  if (err) console.log(err);
  console.log("connected to the database");
});

app.engine('.hbs', expressHbs({ defaultLayout: 'layout', extname: '.hbs' }));
app.set('view engine', 'hbs');
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const mainRoutes = require('./routes/main');

app.use(mainRoutes);


app.listen(3030, (err) => {
  if (err) console.log(err);
  console.log(`Running on port ${3030}`);
});

secret js.

module.exports = {
  database: ''

}

main.js

const router = require('express').Router();
const User = require('../models/user');

router.get('/', (req, res, next) => {
  res.render('main/landing');

});

router.get('/create-new-user', (req, res, next) => {
  var user = new User();
  user.email = "[email protected]"
  user.name = "Jack";
  user.password = "123456";
  user.save(function(err) {
    if (err) return next(err);
    res.json("Successfully created");
  });
});


module.exports = router;

user js.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
  email: { type: String, unique: true, lowercase: true },
  name: String,
  password: String,
  photo: String,
  tweets: [{
    tweet: { type: Schema.Types.ObjectId, ref: 'Tweet' }
  }]
});

module.exports = mongoose.model('User', UserSchema);

dependencias": {
    “body-parser”: “^1.18.2”,
    “express”: “^4.16.2”,
    “express-handlebars”: “^3.0.0”,
    “hbs”: “^4.0.1”,
    “mongoose”: “^4.13.7”,
    “morgan”: “^1.9.0”
}
  • Edith question and place the code instead of images!

1 answer

0


As quoted by the author by comment, he was getting the following error:

Deprecationwarning: open() is deprecated in Mongoose >= 4.11.0, use openUri() Instead, or set the useMongoClient option if using connect() or createConnection(). Db.prototype.authenticate method will no longer be available in the next major release 3.x as Mongodb 3.6 will only allow auth Against users in the admin db and will no longer allow Multiple crede ntials on a socket. Please authenticate using Mongoclient.connect with auth credentials.

The error message informs that, to fix the problem, you must use the method openUri() or if using the method connect() or createConnection() configure the option useMongoClient as true.

mongoose.connect('mongodb://localhost/bd', { useMongoClient: true })
  • I guess you didn’t get bored. My departments are up to date but I learned to make the connection method in an outdated version.

  • This one: Mongoose.connect(config.database, Function(err) { if (err) console.log(err); console.log("Connected to the database"); });

  • (Ode:7844) Deprecationwarning: open() is deprecated in Mongoose >= 4.11.0, use openUri() Instead, or set the useMongoClient option if using connect() or createConnection(). Db.prototype.authenticate method will no longer be available in the next major release 3.x as Mongodb 3.6 will only allow auth Against users in the admin db and will no longer allow Multiple crede ntials on a socket. Please authenticate using Mongoclient.connect with auth credentials.

  • Then pass the parameter useMongoClient: true, see: mongoose.connect('mongodb://localhost/myapp', { useMongoClient: true })

  • It worked. I repeat the question or I repeat the answer?

  • Windows 7 .....

  • All right. I fixed the answer.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.