猫鼬总是返回一个空数组NodeJS [英] Mongoose always returning an empty array NodeJS

查看:61
本文介绍了猫鼬总是返回一个空数组NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用findfindOne,但都没有返回文档. find返回空数组,而findOne返回null.在nullnull中都是err.

I have tried using find and findOne and both are not returning a document. find is returning an empty array while findOne is returning null. err in both cases in null as well.

这是我的关系:

function connectToDB(){
    mongoose.connect("mongodb://localhost/test"); //i have also tried 127.0.0.1
    db = mongoose.connection;
    db.on("error", console.error.bind(console, "connection error:"));
    db.once("open", function callback(){
        console.log("CONNECTED");
    });
};

这是我的架构:

var fileSchema = mongoose.Schema({
    hash: String,
    type: String,
    extension: String,
    size: String,
    uploaded: {type:Date, default:(Date.now)},
    expires: {type:Date, default:(Date.now()+oneDay)}
});
var Model = mongoose.model("Model", fileSchema);

我的查询在这里:

Model.find({},function(err, file) {
    console.log(err)
    console.log(file);  
});

我可以将内容上传到数据库中,并通过 RockMongo 进行查看,但之后便无法获取它们.这是我第一次使用MongoDB,因此我认为我只是缺少一些基础知识.任何朝着正确方向的推动都是很棒的!

I can upload things to the database and see them via RockMongo but I cannot fetch them after. This my first time using MongoDB so I think I'm just missing some of the fundamentals. Any push in the right direction would be great!

推荐答案

mongoose.model的调用将建立模型所绑定的集合的名称,默认值为

The call to mongoose.model establishes the name of the collection the model is tied to, with the default being the pluralized, lower-cased model name. So with your code, that would be 'models'. To use the model with the files collection, change that line to:

var Model = mongoose.model("Model", fileSchema, "files");

var Model = mongoose.model("file", fileSchema);

这篇关于猫鼬总是返回一个空数组NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆