How to enable UnhandledPromiseRejectionWarning in mongoose?

I am trying to extract data using mongoose.

So, every time I had to receive messages from api - localhost: 3000 / api / posts - I get a foll error that I cannot decrypt.

(node: 12100) UnhandledPromiseRejectionWarning: raw promise rejection (r
ejection id: 1): [MongoError: connect ETIMEDOUT xxxx]

The following is my code in api.js. I would appreciate it if you could give recommendations on where I am mistaken.

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const post = require('../models/post');

const db = "mongodb://<dbuser>:<dbpassword>@xxxxxxx.mlab.com:xxxxxx/xxxxxx";

mongoose.Promise = global.Promise;
mongoose.connect(db, function(err) {
    if(err) {
        console.log('Connection error');
    }
});

router.get('/posts', function(req, res) {
    console.log('Requesting posts');
    post.find({})
        .exec(function(err, posts) {
            if (err) {
                console.log('Error getting the posts');
            } else {
                res.json(posts);
                console.log(posts);
            }
        });
});
//in order for server.js file to access the api, we have to add the foll line
module.exports = router;

May 23, 2017

Now I also get an obsolescence warning when I actually turned on foll loc:

mongoose.Promise = global.Promise; //we add this because if we dont, you may get a warning that mongoose default promise library is deprecated

I would be grateful if I could get some recommendations on this issue. Thanks

+5
4

, :

 router.get('/posts', function(req, res) {
    console.log('Requesting posts');
    post.find({})
        .exec()
        .then(function(posts) {
            res.json(posts);
            console.log(posts);
        })
        .catch(function(error){
            console.log('Error getting the posts');
        });
});

, :

router.get('/posts', function(req, res) {
    console.log('Requesting posts');
    post.find({}, function(err, posts){
        if (err) {
            console.log('Error getting the posts');
        } else {
            res.json(posts);
            console.log(posts);
        }
    })
});
+2

- catch.

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI).catch(function (reason) {
    console.log('Unable to connect to the mongodb instance. Error: ', reason);
});
0

Adding my answer as others does not give a clear picture.

As you make mongooseavailable as a global promise, mongoose.Promise = global.Promiseyou will have to process the promise using .then()and.catch()

Here's how:

...
mongoose.Promise = global.Promise;
mongoose.connect(db)
  .then(res => console.log("Connected to DB"))
  .catch(err => console.log(err))
...
0
source

workaround => use event listener

   mongoose.connect('mongodb://localhost:27017/yourDB', {
      useNewUrlParser: true
    });
    mongoose.connection.on('error', err => {
      throw 'failed connect to MongoDB';
    });
0
source

Source: https://habr.com/ru/post/1677669/


All Articles