回调没有在nodejs中获取记录

时间:2018-03-22 14:37:28

标签: node.js mongodb api mongoose node-modules

我想在postman中传递电子邮件,并希望将提取的json data打印到console。但它不是fetching any data。所以帮我解决这个问题

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.email=function(r){
    get().dbo.collection("doctor").find({"email":r}).toArray(function(err,result)
    {
        if(err) throw err;
        console.log(result)
        return result;
    })
}

doctor.js

var express = require('express');
var router = express.Router();

var bodyParser = require("body-parser");
var validator = require('validator');
var mongo= require('./mongoconnect')
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
router.post('/',function(req,res)
    {
        d=mongo.email(req.body.email)
        console.log(d);
    })

module.exports = router;

1 个答案:

答案 0 :(得分:2)

export.email中,您有一个拼写错误

exports.email = function(r) {
    get().collection("doctor").find({"email":r})  // no ".dbo" after get()