Retrieving data from mlab heroku Retrieving data from mlab heroku heroku heroku

Retrieving data from mlab heroku


From what I can see in the code. complaintController will be used by the express.js router, Am I correct?

I also see in the complaintModel.js is that the get function you've exported requires 2 parameters which are a filter & a limit. But in the Controller file you're not providing any of those arguments at all.

I haven't tested this myself yet but try changing your complaintModel.js to this

var mongoose = require("mongoose");var complaintSchema = mongoose.Schema({  name: {    type: String,    required: true  }});var Complaint = mongoose.model("master_complaint", complaintSchema);// Exports the get functionmodule.exports.get = function(filter, limit, callback) {  var mongoDB = "MongoDB URI";  var connection = mongoose.createConnection(mongoDB, {    User: "username",    Password: "pass"  });  connection.on("open", function() {    console.log("connection established!!!");    Complaint.find(filter)      .limit(limit)      .exec()      .then(results => {          callback(undefined, results)      })      .catch(err => {          console.log(err);          callback("ERROR: Can't query the collection", undefined)      });  });};

And change the complaintController.js to the following

var Complaint = require("./complaintModel");module.exports.index = function(req, res) {  var params = req.query;  const filter = params.filter;  const limit = params.limit;  Complaint.get(    filter,    limit,    (err,    complaints => {      if (err) {        res.json({          status: "error",          message: err        });      } else {        res.json({          status: 200,          message: "Complaints retrieved successfully",          data: complaints        });      }    })  );};