0

I am pretty new to mongodb and backend development in general, and I am having difficulty trying to query my mongodb. I am simply trying to create a route that returns all the users in "users" database that have a bullets property greater than 0. I will post my users model below.

Right now in my getUsers controller, I am simply trying to console.log the result, to no success. Eventually what I am trying to achieve is this getUsers route will be triggered in my indexRouter.js by my /dashboard route. Then I will need to pass the result to the client. Again, I am pretty new to express and back end dev so any help would be great!

I am including the github repo for any server and/or config related issues, outside of the files I posted here on stack. The .js files I have examples of on stack are more current than my github repo files, but all other files should be the same.

Full Github Repo

userModel.js

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  },
  bullets: {
    type: Number,
    default: 0
  }
});

const User = mongoose.model('User', UserSchema);

module.exports = User;

userController.js (i didnt include all the controllers just the one I am trying to work on right now)

const User = require('./userModel');
const passport = require('passport');
const bcrypt = require('bcryptjs');


exports.getUsers = (req, res, next) => {
console.log(User.find({ bullets: { $gt: 0 } }))
}

userRoutes.js

const express = require('express');
const router = express.Router();
const controller = require('./userController');

router.route('/login')
  .get(controller.getLogin)
  .post(controller.postLogin)

router.route('/register')
  .get(controller.getRegister)
  .post(controller.postRegister)

router.route('/logout')
  .get(controller.getLogout)

router.route('/buyBullet')
  .get(controller.buyBullet);

router.route('getUsers')
  .get(controller.getUsers);




module.exports = router;

indexRouter.js

const express = require('express');
const router = express.Router();
const { ensureAuthenticated } = require('../../config/auth');


router.get('/dashboard', ensureAuthenticated, (req, res, next) => {
  res.render('dashboard', {
    name: req.user.name
  });
});

module.exports = router;

Jordan
  • 176
  • 10

1 Answers1

1

The reason is because Model.find() is an asynchronous function so when you console.log the function call it will log the promise returned by the call. If you want to use it as middleware I suggest making the controller.getUsers function asynchronous and awaiting the Model.find() call

userController.js:

exports.getUsers = async (req, res, next) => {
    console.log(await User.find({ bullets: { $gt: 0 } }))
}
Abir Taheer
  • 2,502
  • 3
  • 12
  • 34
  • that code snippet isnt working for me but I was thinking it had to be an async issue causing the problem. – Jordan Feb 09 '21 at 05:13
  • 1
    @Jordan I have a theory that it might have to do with your ensureAuthenticated middleware. Can you edit the post and add in the code for that – Abir Taheer Feb 09 '21 at 05:18
  • 1
    Nevermind, I looked through the repo and that isn't the issue – Abir Taheer Feb 09 '21 at 05:19
  • Right now I am just trying to test the route, so that when I hit the route in my browser it simply console.logs all the users with greater than 0 bullet count. It isnt being triggered by my dashboards route yet but that is the end goal. – Jordan Feb 09 '21 at 05:22
  • WOOOOWWWWW. Look at my usersRoutes.js file. There is no / in front of my URI. LOL. – Jordan Feb 09 '21 at 05:37