Node.js + Mongoose - 出了什么问题?

时间:2021-10-22 04:01:29

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.

我一直在研究node.js + mongoose简单的应用程序,然而,出了点问题。我一直在根据这个网站的一些例子工作,但根本没有幸运。但是,代码运行时,没有从db(已填充,btw)返回任何记录。有人可以看看并给我一些帮助吗?我现在正在研究这一天超过一天,但没有快乐。

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

最后,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

谢谢和问候,Vinicius

1 个解决方案

#1


1  

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

根据文档,Model.find()的第一个参数是条件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);
    });
};

#1


1  

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

根据文档,Model.find()的第一个参数是条件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);
    });
};