【问题标题】:Retrieving data from mlab heroku从 mlab heroku 中检索数据
【发布时间】:2019-08-02 16:53:25
【问题描述】:

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

现在,我需要对 mongoose 连接进行必要的更改,因为我要从本地数据库迁移到 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);
  }
});

当我提出获取请求时,我收到以下错误我知道投诉模块存在导出问题,但任何建议或想法都会有所帮助。

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)

【问题讨论】:

    标签: mongodb express heroku mongoose


    【解决方案1】:

    根据我在代码中看到的内容。 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
            });
          }
        })
      );
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-17
      • 2017-06-05
      • 2023-03-29
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      相关资源
      最近更新 更多