Mongodb 从mlab heroku检索数据

Mongodb 从mlab heroku检索数据,mongodb,express,heroku,mongoose,Mongodb,Express,Heroku,Mongoose,我按照教程学习了从MongoDB执行基本CRUD操作的Express.js应用程序。本地创建的所有操作都可以正常工作。 作为下一步(不在教程中),我需要集成Heroku为MongoDB提供的mLab,以便将应用程序推送到Heroku 现在,我需要对mongoose连接进行必要的更改,因为我正在从本地数据库迁移到mLab。我做了必要的更改,但现在应用程序抛出了一个错误 complaintController.js(获取请求并使用模型的类) complaintModel.js(本地MongoDB这很

我按照教程学习了从MongoDB执行基本CRUD操作的Express.js应用程序。本地创建的所有操作都可以正常工作。 作为下一步(不在教程中),我需要集成Heroku为MongoDB提供的mLab,以便将应用程序推送到Heroku

现在,我需要对mongoose连接进行必要的更改,因为我正在从本地数据库迁移到mLab。我做了必要的更改,但现在应用程序抛出了一个错误

complaintController.js(获取请求并使用模型的类)

complaintModel.js(本地MongoDB这很好用

complaintModel.js(连接到mLab会引发错误

在这里,当我发出get请求时,我发现以下错误。我知道投诉模块存在出口问题,但任何建议或想法都会有所帮助

TypeError: Complaint.get is not a function
at exports.index (R:\Workings\PersWork\web\webpack-demo\controller\complaintController.js:6:15)
at Layer.handle [as handle_request] (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\layer.js:95:5)
at next (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\layer.js:95:5)
at R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:281:22
at Function.process_params (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:335:12)
at next (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:275:10)
at Function.handle (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:174:3)
at router (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:317:13)
at R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:284:7
at Function.process_params (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:335:12)
at next (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:275:10)
at jsonParser (R:\Workings\PersWork\web\webpack-demo\node_modules\body-parser\lib\types\json.js:110:7)

从代码中我可以看到complaintController将由express.js路由器使用,对吗

我在complaintModel.js中还看到,您导出的get函数需要两个参数,即过滤器和限制。但是在控制器文件中没有提供任何这些参数

我自己还没有测试过这个,但是试着把你的complaintModel.js改成这个

var mongoose = require("mongoose");

var complaintSchema = mongoose.Schema({
  name: {
    type: String,
    required: true
  }
});

var Complaint = mongoose.model("master_complaint", complaintSchema);

// Exports the get function
module.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)
      });
  });
};
并将complaintController.js更改为以下内容

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
        });
      }
    })
  );
};
TypeError: Complaint.get is not a function
at exports.index (R:\Workings\PersWork\web\webpack-demo\controller\complaintController.js:6:15)
at Layer.handle [as handle_request] (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\layer.js:95:5)
at next (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\layer.js:95:5)
at R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:281:22
at Function.process_params (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:335:12)
at next (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:275:10)
at Function.handle (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:174:3)
at router (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:317:13)
at R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:284:7
at Function.process_params (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:335:12)
at next (R:\Workings\PersWork\web\webpack-demo\node_modules\express\lib\router\index.js:275:10)
at jsonParser (R:\Workings\PersWork\web\webpack-demo\node_modules\body-parser\lib\types\json.js:110:7)
var mongoose = require("mongoose");

var complaintSchema = mongoose.Schema({
  name: {
    type: String,
    required: true
  }
});

var Complaint = mongoose.model("master_complaint", complaintSchema);

// Exports the get function
module.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)
      });
  });
};
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
        });
      }
    })
  );
};