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.