I don't know if it is possible or maybe I don't know how to quite google the question properly but I am wondering is their a way to query the MongoDB from one file and return the results to another using node. Lets say I have two files routes.js and helpers.js
routes.js
const finder = require('../db_helpers/userHelpers');
exports.getIndex = (req, res, next) => {
finder.findUser()
.then(user => {
if (!user) {
return res.redirect('/signup')
}
res.render('shop/landing', {
pageTitle: 'landing'
});
})
.catch(err => {
console.log(err)
})
};
helpers.js
const User = require('../models/user');
exports.findUser = (user) => {
User.findOne()
.then(user => {
console.log(user);
return user
})
.catch(err => {
return err
})
};
This is what I have been working with for a few hrs now changing things around and such but to no avail. Like I said I may have been googling wrong but if someone could point me in the right direction or tell me this isn't possible that would be greatly appreciated.