0

When I run this program controls never enters into this line, even though the condition is true, it always executes else condition.

if(result4[0].doctor_id==doctor_id || result4[0].charge_id==transfer)

patient_update.js

var express = require('express');
var router = express.Router();
var app = express();
var bodyParser = require("body-parser");
var validator = require('validator');
var mongo= require('./mongoconnect')
var authentication=require('./check_authentication')
var ObjectId=require('mongodb').ObjectId
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
router.put('/',authentication.check, function(req,res)
{

    var errobj=
    {
        "message":"",
        "status":"",
        "error" :""
    }
    var result = res.locals.result;
    var doctor_id=result[0]._id;
    var patient_name = req.body.patient_name;
    var charge_id = ObjectId(req.body.charge_id);
    var transfer = ObjectId("5ab243ac73959deb3ad79fec");
    var patient_id = ObjectId(req.body.patient_id)

    if(req.body.patient_id=="undefined" || req.body.patient_id=="" || req.body.patient_id==null)
    {
        errobj.message = "not found";
        errobj.status = 400
        errobj.error = "you must give a valid patient_id"
        res.json(errobj)
    }else{
            mongo.get().collection("patient").find({"_id":patient_id}).toArray(function(err,result4)
            {
                if(result4.length==1)
                {
                    console.log(result4)
                    console.log(result)
                    // console.log(result1[0].charge_id)
                    if(result4[0].doctor_id==doctor_id|| result4[0].charge_id==transfer)
                    {
                        console.log("ll")
                        if((validator.isAlpha(patient_name)) && (patient_name.length>=3))
                        {
                            if(validator.isAlpha(charge_level))
                            {
                                 mongo.get().collection("charge").find({_id : charge_id}).toArray(function (err, result2)
                                 {
                                    if(result2.length==1)
                                    {
                                            if(err) throw err;

                                            mongo.get().collection("patient").updateOne(
                                            { _id : req.body.patient_id },
                                            {
                                            $set: {patient_name: req.body.patient_name,
                                                   charge_id: req.body.charge_id,
                                                   doctor_id:result[0]._id}
                                            })
                                            errobj.message = "success";
                                            errobj.status = 200
                                            errobj.error = "patient record updated successfully"
                                            res.json(errobj)
                                    }
                                    else
                                    {
                                        errobj.message = "not found";
                                        errobj.status = 400
                                        errobj.error = "Patient charge is not valid"
                                        res.json(errobj)
                                    }
                                 })
                            }else{
                                errobj.message = "alphabets only allowed";
                                errobj.status = 400
                                errobj.error = "Patient charge level can only contain alphabets"
                                res.json(errobj)
                            }
                        }else{
                            errobj.message = "Patient name is not valid";
                            errobj.status = 400
                            errobj.error = "patient name should contain characters of length 3 and above"
                            res.json(errobj)
                        }
                    }else{
                        errobj.message = "unauthorized doctor";
                        errobj.status = 400
                        errobj.error = "you are not allowed to access this patient record"
                        res.json(errobj)
                    }
                }else{
                  errobj.message = "not found";
                  errobj.status = 400
                  errobj.error = "Patient id not found"
                  res.json(errobj)
                }
            })
        }
})

module.exports = router;

and Mongo DB collections I have used in this program are:

doctor

{ 
    "_id" : ObjectId("5ab6296be2f07836114c2533"), 
    "name" : "kkk", 
    "hospital_id" : ObjectId("5aa92df0ec6b3cc78ff88afd"), 
    "gender" : "Male", 
    "designation" : "aaaaaaa", 
    "email" : "[email protected]", 
    "pwd" : "$2a$08$fQmZM1CS7mh9sQdGZh3Rd.YiurdVD9c2cWNpHkch82W8OQqzrgKZC", 
    "token" : "T88ykZS8Oi"
}
{ 
    "_id" : ObjectId("5ab66efafc7b14106d59580c"), 
    "name" : "kkk", 
    "hospital_id" : ObjectId("5aa92df0ec6b3cc78ff88afd"), 
    "gender" : "Male", 
    "designation" : "aaaaaaa", 
    "email" : "[email protected]", 
    "pwd" : "$2a$08$ZSk7S7Dj0OpUbV0RBb7afOGbGicSfxyBL9L3N8L6qxB2NtuyfsODG", 
    "token" : "9uSWStZcCG"
}

patient

{ 
    "_id" : ObjectId("5ab659b14832893e12bf17e5"), 
    "patient_name" : "kkkkkkkkkkjjj", 
    "charge_id" : ObjectId("5ab243ac73959deb3ad79fec"), 
    "description" : "suffering from fever", 
    "gender" : "male", 
    "doctor_id" : ObjectId("5ab6296be2f07836114c2533")
}
{ 
    "_id" : ObjectId("5ab65be6fed5043f3a010150"), 
    "patient_name" : "kkkkkkkkkkjjj", 
    "charge_id" : ObjectId("5aaa62cb9ae2a2a642b7495e"), 
    "description" : "suffering from fever", 
    "gender" : "male", 
    "doctor_id" : ObjectId("5ab6296be2f07836114c2533")
}
{ 
    "_id" : ObjectId("5ab65d54fed5043f3a010151"), 
    "patient_name" : "kkkkkkkkkkjjj", 
    "charge_id" : ObjectId("5aaa62cb9ae2a2a642b7495e"), 
    "description" : "suffering from fever", 
    "gender" : "male", 
    "doctor_id" : ObjectId("5ab6296be2f07836114c2533")
}
{ 
    "_id" : ObjectId("5ab65ef3b3c3f640ced0b6c3"), 
    "patient_name" : "kkkkkkkkkkjjj", 
    "charge_id" : ObjectId("5aaa62cb9ae2a2a642b7495e"), 
    "description" : "suffering from fever", 
    "gender" : "male", 
    "doctor_id" : ObjectId("5ab6296be2f07836114c2533")
}
{ 
    "_id" : ObjectId("5ab66f8dfc7b14106d59580e"), 
    "patient_name" : "kkknnnn", 
    "charge_id" : ObjectId("5aaa62cb9ae2a2a642b7495e"), 
    "description" : "suffering from fever", 
    "gender" : "male", 
    "doctor_id" : ObjectId("5ab66efafc7b14106d59580c")
}
{ 
    "_id" : ObjectId("5ab671dd62871b152762963f"), 
    "patient_name" : "neww", 
    "charge_id" : ObjectId("5aaa62cb9ae2a2a642b7495e"), 
    "description" : "suffering from fever", 
    "gender" : "male", 
    "doctor_id" : ObjectId("5ab66efafc7b14106d59580c")
}

charge

{ 
    "_id" : ObjectId("5aaa2a7c880a1ec007584706"), 
    "charge_level" : "admit"
}
{ 
    "_id" : ObjectId("5aaa628e9ae2a2a642b7495c"), 
    "charge_level" : "tolerable"
}
{ 
    "_id" : ObjectId("5aaa62b79ae2a2a642b7495d"), 
    "charge_level" : "severe"
}
{ 
    "_id" : ObjectId("5aaa62cb9ae2a2a642b7495e"), 
    "charge_level" : "critical"
}
{ 
    "_id" : ObjectId("5ab243ac73959deb3ad79fec"), 
    "charge_level" : "transfer"
}

check_authentication.js

 var mongo= require('./mongoconnect')


    exports.check = function (req,res,next) {
     mongo.get().collection("doctor").find({"token":req.body.token}).toArray(function(err,result)
        {
            if(err) throw err;
            console.log(result)

            if (result.length == 0)  
            {
                res.json({message:'you must login first'})
            } else 
            { 
              res.locals.result = result;
                next()
            }
        });
    }
3
  • what is res.locals.result? where is it coming from? Commented Mar 24, 2018 at 17:23
  • @JSilv it is within the check_authentication file, wait I'll update that file also in question Commented Mar 24, 2018 at 17:41
  • @JSilv now I update that file too, in question please check Commented Mar 24, 2018 at 17:49

1 Answer 1

1

ObjectId is an object and you cannot just "equals" with ==. You should use .equals method:

objectId1.equals(objectId2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.