MEAN STACK: Mongoose model blocks communication with my controller

-1
My controller does not send data to my router when my Model is being exported into the application and I do not know why this happens.

controller.js

var Model = require('../models/dado.js');

exports.listaDados = function(req, res) {
  Model.find({}, function(erro, lista) {
    if(erro) console.log(erro);
    res.json(lista);
  });
};

data.js

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var schema = new Schema({
  nome: {type: String, required: true},
  idade: {type: Number, required: true, index: {unique: false}}
});

module.exports = mongoose.model('Dado', schema);

package.json

{
  "name": "teste",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.17.2",
    "ejs": "^2.5.6",
    "express": "^4.15.3",
    "express-load": "^1.1.15",
    "method-override": "^2.3.9",
    "mongodb": "^2.2.27",
    "mongoose": "^4.10.4"
  }
}

router.js

var controller = require('../controllers/controller');

module.exports = function(app) {
  app.get('/dados', controller.listaDados);
};

If I comment on the part of the code that exports the Model , type:

//module.exports = mongoose.model('Dado', schema);

My controller works fine, I can even send static server data to the client, however I need Model to get the bank's data.

  

Note: Mongoose connects normal, everything works OK except for that part.

    
asked by anonymous 01.06.2017 / 13:59

1 answer

1

By the code displayed in the github Gist link, the mongoose configuration file is missing.

Your given file, is missing to make require of your mongoose configuration file.

ex: var db = require('./meu_arquivo_de_config_do_mongo');

Getting something similar to this.

var db = require('./meu_arquivo_de_config_do_mongo');
var mongoose = require('mongoose');
var dadoSchema = new mongoose.Schema({
    // ...
});

module.exports = db.model('Dado',  dadoSchema);

Notice that you create a new Schema to add the existing instance of mongoose. In the previous code you do not pass this instance to anyone.

    
01.06.2017 / 14:19