0
我想用真/假条件广东话从数据库中提取数据
这里来从MongoDB的一些数据是回调函数:
module.exports.getAllTeachersByValidation = function (condition, fields, options, callback){
const query = {
account: {
valid: condition
}
};
Teacher.find(query, fields, options, callback);
};
这里是方案:
const teacherSchema = mongoose.Schema({
account: {
username: {
type: String,
required: true,
unique: true,
lowercase: true
},
password: {
type: String,
required: true
},
valid: {
type: String,
enum: ["true","false"],
required: true,
lowercase: true
}
},
,代码:
const condition = req.query.condition;
const ret_values = "_id "
+ "ID "
+ "account.username "
+ "account.valid "
+ "name "
+ "communication.email "
+ "communication.phone";
if(typeof condition !== 'undefined'){
console.log("Sending teachers by validation: " + condition);
Teacher.getAllTeachersByValidation(condition.toLowerCase(), ret_values, {}, (error, teachers) => {
if (error) throw error;
if(teachers){
return res.json({
success: true,
teachers
});
}else{
return res.json({
success: false,
msg: "Error retrieving teachers",
error: "No teacher to be found"
});
}
});
}
我试过有效的时候是一个字符串类型,当有效的时候是一个布尔类型,但我总是得到一个空教师的数组作为结果(成功:真)
有人可以解释为什么.find()不返回数组?或者我的问题在哪里?
当我不使用条件字段它返回所有教师(包括当有效是假/真)