Mongoose not working

This is my first time writing an MVC application in Node / Express / Mongoose, so I can really take some help. My .find () command just doesn't find anything !: (

The structure is that I have a folder / application in the root. / app contains / models (circuits), / controllers and / views in it. And I have app.js outside at the root.

Somewhere in app.js:

// all necessary config/setup stuff.. var mongoose = require('mongoose'); mongoose.connect(config.db); var app = express(); require('./config/routes')(app) 

In the routes.js file:

 var skills = require('../app/controllers/skills'); app.get('/', skills.showall); 

My skills.js controller contains:

 var Skill = require('../models/skill'); exports.showall = function(req, res) { Skill.find({}, function(err, docs){ if (!err) { res.render('index', {title: 'Skilldom', skills: docs}); } else { throw err; } }); } 

Finally, my skill.js model contains:

 var mongoose = require('mongoose'); //Skill schema definition var skillSchema = new mongoose.Schema({ name: String, length: String, }); var Skill = mongoose.model('Skill', skillSchema); module.exports = Skill; 

My index view displays, so I see the contents from my index.jade template, but for some reason the find command in the model does not get anything. I can confirm that my database (in MongoHQ) has real data.

Any thoughts?

+6
source share
2 answers

Change Skill.js for this

 var mongoose = require('mongoose'); mongoose.set('debug', true); //Skill schema definition var skillSchema = new mongoose.Schema({ name: String, length: String, }); var Skill = mongoose.model('Skill', skillSchema); module.exports = Skill; 

After that, you can see on the console if mongoose is fulfilling your requests.

+10
source

I was in the same situation as you describe, and it turned out that I did not understand the magic of the names of the mongoose collection, in your code it will try to load the β€œskills”, and if this is not what he called in your mongo, nothing will be returned . It should really throw a "such a collection" error instead of imho.

+2
source

Source: https://habr.com/ru/post/956528/


All Articles