0

I'm trying to get a result from MongoDB database return as a response for my GET Request when I make the call I'm getting undefined in the response, I'm able to see the result in console log on the database side of the code. I'm feeling like it might have something to do with it being an array

MongoClient = require('mongodb'),
  mongodburl = "mongodb://localhost:27017",
  mongo = require('mongodb'),
  assert = require('assert');


let method={}
method.insertdata=function(data,collectionpara) {
  MongoClient.connect(mongodburl, function (err, db) {
    assert.equal(null, err);
    let dbo = db.db("hor-usData");
    let myobj = data;
    dbo.collection(collectionpara).insert(myobj, function (err, result) {
      assert.equal(null, err);
      console.log("insert data");
      db.close();
    })
  })
}

method.deleteRecords=function(collectionpara) {
  MongoClient.connect(mongodburl, function (err, db) {
    assert.equal(null, err);
    let dbo = db.db("hor-usData");
    var myquery = {
      title: /^B/
    };
    dbo.collection(collectionpara).deleteMany(myquery, function (err, obj) {
      if (err) throw err;
      console.log(obj.result.n + " document(s) deleted");
      db.close();
    })
  })
}
method.getdata=function(collectionpara){
  MongoClient.connect(mongodburl, function(err, db) {
    if (err) throw err;
    let dbo = db.db("hor-usData");
    dbo.collection(collectionpara).find().toArray(function(err, result) {
      if (err){
        return reject(err)
      }
      console.log('result',result);
      return resolve(result);
    });
  });  
}

module.exports = method;

and my router code

let assert = require('assert'),
    express = require('express'),
    router = express(),
    swaggerUi = require('swagger-ui-express'),
    database=require('../databaseCon'),
   // swaggerDocument = require('./swagger.json'),
    utils = require('../utils/utils');
var port = process.env.PORT || 3000;


//get donedeal
router.get('/getDonedeal', function (req, res, next) {
    let donedealResult = []
    donedealResult=database.getdata('donedeal');
    res.send(donedealResult);
});

//get carzone
router.get('/getCarzone', function (req, res, next) {
    let carzoneResult = [];
    carzoneResult=database.getdata('carzone');
    res.send(console.log(carzoneResult));
});


router.get('/cars', function (req, res, next) {
    res.router('Mainrouter')
});

router.get('/getCarzone', function (req, res, next) {
    var resultArray = []
});
router.listen(port);
//app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//app.use('/api/v1', router);
module.exports = router;
soad666p
  • 11
  • 5
  • 1
    It has already been answered on this post : [Node.js MongoDB collection.find().toArray returns nothing](https://stackoverflow.com/questions/45118957/node-js-mongodb-collection-find-toarray-returns-nothing) – Sense Mar 05 '19 at 14:48
  • the problem I'm having seems to be more on the express side, I can data no problem but getting the data to show when I make API call is not working – soad666p Mar 05 '19 at 15:39
  • 1
    @Sense is right: your answer is in the validated answer of the linked post. Your `method.getdata` should return a promise (instead of implicitly returning `undefined`), and this promise should be resolved with `result`, inside `toArray`'s callback. – Stock Overflaw Mar 05 '19 at 16:42
  • @Sense, I update the database code, I'm still getting the same error on the express side when I made the /getDonedeal call it showing as undefine – soad666p Mar 06 '19 at 23:29
  • You did not create any Promise in your `getdata` function, so you can't `resolve` or `reject` anything. Please read carefully the link I provided earlier. – Sense Mar 07 '19 at 09:13

0 Answers0