从Mlab Heroku检索数据

时间:2019-03-12 09:10:52

标签: mongodb express heroku mongoose

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

现在,由于我正从本地数据库移至mLab,因此需要对猫鼬连接进行必要的更改。我进行了必要的更改,但现在应用程序引发错误。

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

Complaint = require('./complaintModel');
exports.index = function (req, res) {
Complaint.get(function (err, complaints) {      //GET function
    if (err) {
        res.json({
            status: "error",
            message: err,
        });
    }
    res.json({
        status: 200,
        message: "Complaints retrieved successfully",
        data: complaints
    });
});
};

complaintModel.js(本地MongoDB可以正常工作

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


 // Export Complaint model

 var Complaint = module.exports = mongoose.model('complaint', complaintSchema);
 module.exports.get = function (callback, limit) {
     Complaint.find(callback).limit(limit);
 }

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

var mongoDB = "MongoDB URI";

var connection = mongoose.createConnection(mongoDB,
{    
  User: 'username',
  Password: 'pass'
});

var Complaint;
connection.on('open', function() {
  console.log('connection established!!!');
  Complaint = module.exports = connection.model('master_complaint', complaintSchema);
  module.exports.get = function (callback, limit) {
    Complaint.find(callback).limit(limit);
  }
});

在我发出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)

1 个答案:

答案 0 :(得分:0)

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

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

我还没有对此进行测试,但是尝试将您的 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
        });
      }
    })
  );
};