添加过滤器时,Mongoose find / findOne始终返回null

时间:2016-04-22 12:14:53

标签: node.js mongoose

我有过这几天一直困扰着的问题。我为mongoose项目设置nodejs我已经定义了所有模式和模型,如下所示

var studentSchema = new Schema({
    fullname: {type: String, required: true},
    student_id: {type: String, required: true, unique: true},
    votingNo: {type: Number, required: true, unique: true},
    voted: {type: Boolean, required: true, default: false}
});
var Student = mongoose.model('Student', studentSchema, 'student');

我已导出模型,并且我已在另一个模块中使用它。 每当我尝试查询结果时如此:

 model.Student.find({}, function (err, students) {
    console.log(err);
    console.log(students);
});

我得到了结果。但是我添加过滤器的那一刻就像这样:

model.Student.find({student_id: studentId}, function (err, students) {
    console.log(err);
    console.log(students);
});

结果总是一个空数组。

我已尝试使用findOne(),但它总是返回null。

1 个答案:

答案 0 :(得分:1)

尝试调用此类查询

var Student = mongoose.model('Student');
Student.find({}, function (err, students) {
     console.log(err);
     console.log(students);
});

如果它不起作用,请在通话前添加此项,以确保数据库已打开。

var mongoose = require('mongoose');
console.log(mongoose.connection.readyState); // Should not return 0

希望它有所帮助!

相关问题