Wednesday 7 June 2017

My MongoDB code is generating a "server instance pool was destroyed error"

I keep getting this error whenever I try to make a post request in Postman:

C:\Users\gabar\desktop\CheapEat\node_modules\mongodb\lib\utils.js:123
    process.nextTick(function() { throw err; });
                                  ^
MongoError: server instance pool was destroyed

The lines of code, in particular, that are causing are, is this:

  router.post('/register', function(req, res, next){


      db.collection('users').insertOne({
        username: req.body.username,
        password: req.body.password,
        name: req.body.name,
        email: req.body.email,
        profilePic: req.body.profilePic
      }, (err, user) => {
          if (err) throw err;

          bcrypt.genSalt(10, function(err, salt){
              bcrypt.hash(user.password, salt, function(err, hash){
                  if (err) throw err;
                  user.password = hash;
                  user.save();
              })
          });

          user.userId = uuidV4();
        })


        });

For reference, here is the whole file that piece of code is apart of:

const express = require('express');
const passport = require('passport');
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const uuidV4 = require('uuid/v4');
// const verification = require('verification');

let router = express.Router();


const {MongoClient, ObjectID} = require('mongodb');

MongoClient.connect('mongodb://localhost:27017/TodoApp', (err, db) => {
  if (err) {
    return console.log('Unable to connect to MongoDB server');
  }
  console.log('Connected to MongoDB server');

  router.get('/', (req, res, next) => {

      db.collection('users').find({}, (err, users) => {
          if (err) throw err;
          res.json(users);
      })
    });




  router.post('/register', function(req, res, next){


      db.collection('users').insertOne({
        username: req.body.username,
        password: req.body.password,
        name: req.body.name,
        email: req.body.email,
        profilePic: req.body.profilePic
      }, (err, user) => {
          if (err) throw err;

          bcrypt.genSalt(10, function(err, salt){
              bcrypt.hash(user.password, salt, function(err, hash){
                  if (err) throw err;
                  user.password = hash;
                  user.save();
              })
          });

          user.userId = uuidV4();
        })


        });

  router.put('/:userid/updateUserInfo', (req, res, next) => {

    let query = {userId: req.params.userid};

    db.collection('users').findOneAndUpdate(query,
      {$set: {
        profilePic: req.body.profilePic,
        name: req.body.name,
      }},
    {
      returnOriginal: false
    }).then(
      (recipe) =>
      res.json(recipe)
    );
  });

  router.delete('/:userid/deleteUser', (req, res, next) => {

    let query = {userId: req.params.userid};

    db.collection("users").findOneAndDelete(query, (err, user) => {
        if (err) throw err;

        //// delete all the users' recipes and reviews


        db.collection('recipes').deleteMany({postedBy: req.params.userId});


        db.collection('recipes').update({}, {$pull: {'reviewsArray': {'postebBy': userId}}});


        res.send('The user ' + user + 'was succesfully deleted!');
    });



  });




  db.close();
});


module.exports = router;

What does this error mean, and how should I resolve it?



via T-Dot1992

No comments:

Post a Comment