0

I have created routes in express to enter patients data to mongodb & to get the data from the database. The expected result is to get patient profile in return. There is some error in the get route due to which I am not able to see patient_profile in response. Kindly help with the same.

// @ route  GET api/patient/:patientId
// @ desc   Get patient by patientId
// @ access Private

router.get('/:patientId', auth, async (req, res) => {
    try {
      const patient_profile = await Patient.findOne({

        patient: req.params.patientId

      }).populate('patient',['name','phonenumber']);
      //console.log(patient);
      console.log(patient_profile);
      if (!patient_profile) return res.status(400).json({ msg: 'Patient not found' });

      res.json(patient_profile);    
    } catch (err) {
      console.error(err.message);
      if (err.kind == 'ObjectId') {
        return res.status(400).json({ msg: 'Profile not found' });
      }
      res.status(500).send('Server Error');
    }
  });

module.exports=router;
const mongoose= require('mongoose');
autoIncrement = require('mongoose-auto-increment');
const config =require('config');
const db=config.get('mongoURI');

var connection = mongoose.createConnection(db);

autoIncrement.initialize(connection);

const PatientSchema = new mongoose.Schema({
    name:{
        type:String,
        required: true
    },
    phonenumber:{
        type:Number,
        required:true
    },
    date: {
        type: Date,
        default: Date.now
    },
    slider_1:{
        type:Number,
        required: true
    },
    slider_2:{
        type:Number,
        required:true
    },
    slider_3:{
        type:Number,
        required:true
    }

});
PatientSchema.plugin(autoIncrement.plugin, {
  model:'Patient',
   field:'patientId',
   startAt:1,
   incrementBy:1
});

module.exports=Patient=mongoose.model('patient',PatientSchema,'patient');
3
  • Why are you using patientId rather than just _id? Is req.params.patientId what you expect and does it exist in the DB? Are they the same types? Commented Jun 20, 2019 at 14:44
  • Hi Dominic yes patientId exists in the database. I have created same using "mongoose-auto-increment". I have added this due to functionality needed in the application to search patient by its ID. console.log(patient_profile) returns null.Please help! Commented Jun 20, 2019 at 14:50
  • @Dominic I tried changing the code to get patient by mongoDb Id i.e. _id.The findone function returns first document can you please help me.I am stuck! Commented Jun 20, 2019 at 15:20

1 Answer 1

1

You are querying based on patient rather than patientId, which is the field you specified to auto increment.

Amend your query to:

const patient_profile = await Patient.findOne({

  patientId: req.params.patientId

}).populate('patient',['name','phonenumber']);

I hope this solves your problem.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Steve.. Thanks a lot for your helps.. This works! I am new to javascript can you pls help me with query to find patient by phonenumber
You just need to pass phonenumber as a property of your query object: Patient.findOne({ phonenumber: '0123456789' })
phonenumber: req.params.phonenumber , I am trying this query. Console returns "null". Kindly help
Hi Steve!Thanks for your support..Figured out my mistake.I was using same route for phonenumber. I tried with new route it worked!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.