vbatista
vbatista

Reputation: 359

Node.js + Mongoose - what's going wrong?

I have been studying a node.js + mongoose simple app, however, something went wrong. I have been working based on some examples from this site, but no lucky at all. The code runs, however, no records are being returned from db(which is populated, btw). Can someone have a look and gimme some help? I am working on this for more than a day now, but with no joy.

models/entidade.js

var mongoose = require('mongoose')
   ,Schema = mongoose.Schema
   ,ObjectId = Schema.ObjectId;

var entidadeSchema = new Schema({
        cnpj:  String,
        razaoSocial: String,
        nomeFantasia:   String,
        endereco: String,
        unidades: [{ codigo: String, nome: String, endereco: String, ativo: {type: Boolean, default: true} }],
        dataCriacao: { type: Date, default: Date.now },
        cadastroAtivo: { type: Boolean, default: false },
    });

module.exports = mongoose.model('Entidade', entidadeSchema);

routes/entidades.js

var Entidade = require('../models/entidade.js');

exports.list = function(req, res) {
    Entidade.find(function(err, entidades) {
        console.log("route IN: %d records", entidades.length );
        res.send(entidades);
    });
};

and finally, server.js

var express = require('express');     
var app = express();

app.configure(function () {
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
});

// connection to mongoDB
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/ccidev');    

var entidade = require('./routes/entidades');

app.get('/entidades/list', entidade.list);

app.listen(3000);

As I said, when running the app, I don't get any errors, but an empty result. Any help is appreciated.

Thanks and regards, Vinicius

Upvotes: 0

Views: 406

Answers (1)

PierrickP
PierrickP

Reputation: 105

According to the documentation, the first argument of Model.find() is the condition Try

Entidade.find({}, function(err, entidades) {});

routes/entidades.js

var Entidade = require('../models/entidade.js');

exports.list = function(req, res) {
    Entidade.find({}, function(err, entidades) {
        console.log("route IN: %d records", entidades.length );
        res.send(entidades);
    });
};

Upvotes: 1

Related Questions