为什么无法获得查询结果

时间:2019-08-24 15:58:08

标签: node.js mongodb

我构建了mongodb服务器并加载了它。 我使用'node api.js'命令加载api服务器 但我无法获得查询结果。 错误和结果返回null。

我检查了mongod,并且env集合有2个文档。 为什么会这样?

var my = new Schema({
            status: Boolean,
            system_mode: Number
    });
var env = mongoose.model('env', my);
env.find().exec(function(err, result){
    console.log(err);
    console.log(result);
    res.send(result);
    return;
});

1 个答案:

答案 0 :(得分:1)

由于预先创建了env模式,因此必须在猫鼬模式中添加{ collection : 'env' },因为mongoose将创建另一个包含复数envs的集合,因此猫鼬模式也应在加载应用程序上一次定义,而不是内部请求处理程序

var my = new Schema({
            status: Boolean,
            system_mode: Number
    }, { collection : 'env' });
var env = mongoose.model('Env', my);



env.find({}).exec(function(err, result){
    console.log(err);
    console.log(result);
    res.send(result);
});
相关问题