Issue Connecting to MongoDB collections

Viewed 60

I am using axios and express.js API to connect to my mongo DB. I have a .get() request that works for one collection and doesn't work for any other collection. This currently will connect to the database and can access one of the collections called users. I have another collection setup under the same database called tasks, I have both users and tasks setup the same way and being used the same way in the code. The users can connect to the DB (get, post) and the tasks fails to connect to the collection when calling the get or the post functions. When viewing the .get() API request in the browser it just hangs and never returns anything or finishes the request.

any help would be greatly appreciated!

The project is on GitHub under SCRUM-150.

API connection
MONGO_URI=mongodb://localhost:27017/mydb

Working

    methods: {
          //load all users from DB, we call this often to make sure the      data is up to date
           load() {
             http
               .get("users")
               .then(response => {
                 this.users = response.data.users;
               })
               .catch(e => {
                 this.errors.push(e);
               });
           },

           //opens delete dialog
            setupDelete(user) {
             this.userToDelete = user;
            this.deleteDialog = true;
      },

      //opens edit dialog
      setupEdit(user) {
        Object.keys(user).forEach(key => {
          this.userToEdit[key] = user[key];
        });
        this.editName = user.name;
        this.editDialog = true;
      },

      //build the alert info for us
      //Will emit an alert, followed by a boolean for success, the type of call made, and the name of the
      //resource we are working on
      alert(success, callName, resource) {
        console.log('Page Alerting')
        this.$emit('alert', success, callName, resource)
        this.load()
      }
    },

    //get those users
    mounted() {
      this.load();
    }
  };


Broken
methods: {
      //load all tasks from DB, we call this often to make sure the data is up to date
      load() {
        http
          .get("tasks")
          .then(response => {
            this.tasks = response.data.tasks
          })
          .catch(e => {
            this.errors.push(e);
          });
      },

      //opens delete dialog
      setupDelete(tasks) {
        this.taskToDelete = tasks;
        this.deleteDialog = true;
      },

      //opens edit dialog
      setupEdit(tasks) {
        Object.keys(tasks).forEach(key => {
          this.taskToEdit[key] = tasks[key];
        });
        this.editName = tasks.name;
        this.editDialog = true;
      },

      //build the alert info for us
      //Will emit an alert, followed by a boolean for success, the type of call made, and the name of the
      //resource we are working on
      alert(success, callName, resource) {
        console.log('Page Alerting')
        this.$emit('alert', success, callName, resource)
        this.load()
      }
    },

    //get those tasks
    mounted() {
      this.load();
    }
  };
2 Answers

Here is my solution: In your app.js, have this:

let mongoose = require('mongoose');

mongoose.connect('Your/Database/Url', {
    keepAlive     : true,
    reconnectTries: 2,
    useMongoClient: true
});

In your route have this:

let mongoose = require('mongoose');
let db = mongoose.connection;
fetchAndSendDatabase('yourCollectionName', db);

function fetchAndSendDatabase(dbName, db) {
    db.collection(dbName).find({}).toArray(function(err, result) {
        if( err ) {
            console.log("couldn't get database items. " + err);
        }
        else {
            console.log('Database received successfully');
        }
    });
}
Related