I'm trying to return a MongoDB Query as a JSON Object so it can be stored in a javascript variable and used as need. I have the query executing fine but I can't get it to store as a JSON object and I keep getting this error Unexpected token u in JSON at position 0. I've looked it up and seen other people having this problem but I tried their solutions and I couldn't get it to work. Please help me.
Here's the user.js model code:
module.exports.example = function() {
var query = User.find({}).lean().exec(function (err, users) {
return JSON.stringify(users);
});
}
and here's my express routes file:
router.get('/everything', function (req, res) {
var q = User.example();
var x = JSON.parse(q);
console.log(x);
});