相关疑难解决方法(0)

Mongoose总是返回一个空数组NodeJS

我尝试过使用find并且findOne两者都没有返回文档.find是在返回一个空数组findOne被返回null.err在这两种情况下null也是如此.

这是我的联系:

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");
    });
};
Run Code Online (Sandbox Code Playgroud)

这是我的架构:

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);
Run Code Online (Sandbox Code Playgroud)

我的查询在这里:

Model.find({},function(err, file) {
    console.log(err)
    console.log(file);  
});
Run Code Online (Sandbox Code Playgroud)

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

mongoose mongodb node.js

18
推荐指数
1
解决办法
1万
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1