I don't need schema validation, so I'd rather not add the weight of Mongoose to my project. Unfortunately, I am unable to find a way in MongoClient; I'm unable to get my function outside of the mongo.connect ({}) scope, so that I can export my callback functions.
const mongo = require('mongodb').MongoClient;
const uri = "mongodb://localhost:27017";
mongo.connect(uri, function (err, db) {
const dbo = db.db("readings");
if (err) {
throw err;
};
console.log('MongoDB connected...')
function insertReading (data) {
dbo.collection("myReadings").insertOne(data, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
});
};
});
exports.newReading = function(req, res) {
insertReading(req.body)
};
exports.myDashboard = function (req, res) {
console.log("test");
}
The "newReading" function would pertain to a POST verb and the "myDashboard" function to a GET verb. Putting my exports inside of the scopes of course will throw errors. How do I get my "insertReading" function to function for my out-of-scope function?