Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose Find not working

This is my first time writing a MVC app in Node/Express/Mongoose so I could really use some help. My .find() command just doesn't find anything! :(

Structure is that I have a an /app folder in the root. /app folder contains /models (schemas), /controllers and /views in it. And I have app.js outside in 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 my routes.js file:

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

My controller skills.js 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 Model skill.js 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 renders, so I see the content from my index.jade template, but for some reason the find command in the model is not fetching anything. I can confirm that my database (in MongoHQ) has real data.

Any thoughts?

like image 538
Pranav Piyush Avatar asked Oct 23 '13 06:10

Pranav Piyush


People also ask

How does find work in Mongoose?

The find() function is used to find particular data from the MongoDB database. It takes 3 arguments and they are query (also known as a condition), query projection (used for mentioning which fields to include or exclude from the query), and the last argument is the general query options (like limit, skip, etc).

Why unique is not working in mongoose?

Mongoose will silently fail to add a unique index when either: The collection already has an index of the same name. The collection already contains documents with duplicates of the indexed field.

Does find return an array mongoose?

find returns an array always.

What does find return in mongoose?

find() function returns an instance of Mongoose's Query class. The Query class represents a raw CRUD operation that you may send to MongoDB. It provides a chainable interface for building up more sophisticated queries. You don't instantiate a Query directly, Customer.


2 Answers

Change your 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 at the console if mongoose is doing your queries.

like image 192
labkode Avatar answered Nov 03 '22 20:11

labkode


I was in the same situation as you describe and it turns out I didn't understand the magic of mongoose collection naming, in your code it will try to load the "skills" and if that's not what it's named in your mongo nothing will be returned. Should really toss a "so such collection" error instead imho.

like image 35
user1391445 Avatar answered Nov 03 '22 21:11

user1391445