Monday, 24 April 2017

Cant fetch data from the DataBase

I'm trying to fetch some data from the mongoDB using a true/false condition

here is the callback function :

module.exports.getAllTeachersByValidation = function (condition, fields, options, callback){
    const query =  {
      account: {
          valid: condition
      }
    };
    Teacher.find(query, fields, options, callback);
};

here is the scheme :

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
        }
    },

And the code :

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"
                });
            }
        });
    }

I have tried when valid is a String type and when valid is a Boolean type , but I always get an empty array of teachers as the result (success: true)

Can someone explain me why the .find() does not return an array ? Or where is my problem here?

when I don't use the condition field it return all teachers (including when valid is false/true)



via Zed Evans

No comments:

Post a Comment