javascriptjquerynode.jsmongodbmongoose

MongoDB User.findOne({email:email})


findOne return null Always

I have "user(hello@gmail.com)" data in the database but User.findOne returns null always. Please Help me. I have a node running on background but it gives me null . req.body gives the email that im passing but findOne isnot working. User.create is working fine. but search queries like find,findById,findOne is not working. SomeOne please help me.

const userSchema = new mongoose.Schema({
 name: {
    type: String,
    required: [true, 'A user must have a unique name'],
    unique: true,
  },
  password: {
    type: String,
    required: true,
    unique: true,
    minlength: 8,
  },
  email: {
    type: String,
    required: [true, 'A user must have a unique name'],
    unique: true,
    lowercase: true,
  }
const User = mongoose.model("User", userSchema);

exports.getTest=async (req,res)=>{
    const {email} = req.body  // returns "hello@gmail.com"
    const user = await User.findOne({email})
    console.log(user) //returns null
    res.json({
     
        status:"success",
        data:{
            user
        }
    })

}

Solution

  • Documents should save active: true in the database. but instead, I'm saving every document as false as the default value. So, this is my answer if anyone in the future finds it useful let me know. Take care and goodbye