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;