mongodb的objectid在nodejs程序中不起作用

时间:2018-03-24 16:12:22

标签: node.js mongodb express mongoose mongodb-query

当我运行此程序时,控件永远不会进入此行,即使条件为真,它也始终执行其他条件。

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;
我在这个程序中使用的

和Mongo DB集合是:

医生

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

患者

{ 
    "_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")
}

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

1 个答案:

答案 0 :(得分:1)

ObjectId是一个对象,你不能只与==“等于”。 您应该使用.equals方法:

objectId1.equals(objectId2)