当我从Postman的1家医院objectid
到此计划时,它只返回empty array
。但是one data
匹配objectid
。你能帮帮我解决这个问题吗?当我尝试在控制台中调试程序时,我看到了array is empty
。
doctor.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 validate = require('./validation')
var ObjectId=require('mongodb').ObjectId
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
router.post('/',function(req,res)
{
if(validate.name(req.body.name) && validate.length(req.body.name) && validate.empty(req.body.name))
{
if(validate.empty(req.body.hospital_id))
{
mongo.find("hospital","_id",ObjectId(req.body.hospital_id),function(result1)
{
if(result1.length==1)
{
res.send("succcess")
}
})
}
}
})
module.exports = router;
MongoDB的集合是:
医院:
{
"_id" : ObjectId("5aa92df0ec6b3cc78ff88afd"),
"name" : "apollo_hospitals"
}
{
"_id" : ObjectId("5aa92df9ec6b3cc78ff88afe"),
"name" : "fortis"
}
mongoconnect.js:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var dbo=null;
exports.connection=function()
{
if(dbo!=null) return
MongoClient.connect(url, function(err, db)
{
if (err) throw err;
dbo = db.db("hospital_api");
});
}
var get = function (){
return dbo;
}
exports.find=function(collections,key,value,callback)
{
get().collection(collections).find({key:value}).toArray(function(err,result)
{
// console.log(collections)
// console.log(key)
// console.log(value)
if(err) throw err;
console.log(result)
callback(result)
})
}
答案 0 :(得分:0)
在这里,我自己得到了解决方案。
我们必须使用ObjectID
关键字声明New
..
Var some_variable = New ObjectID(_id)
之后,我们可以使用some_variable