0

It seems the code is ok to me, but I get an empty array. Can anyone please help me on this. I watched many tutorials and I can't understand what I'm doing wrong here. Really appreciate your help

DruginformationController.js

    var druginformation = require('../models/DruginformationModel');

module.exports = {
get: function (req, res) {
    druginformation.find({}).exec(function (err, result) {
        res.send(result);
    })
}
}

DruginformationModel.js

var mongoose = require('mongoose');

module.exports = mongoose.model('druginformation',{
Category:String,
Type:String,
Name:String,
Price:Number,
Reorderlevel:Number,
Dangerlevel:Number,
Remarks:String
});

server.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

app.use(bodyParser.json());
app.use(cors);

var druginformation = require('./controllers/DruginformationController');
app.get('/api/getdruginformation',druginformation.get);

mongoose.Promise = global.Promise;
mongoose.connect("mongodb://root:root@xxxx.mlab.com:xxxx/xx-xx", function (err, db) {
if (!err) {
    console.log("we are connected to mongo");
}
})

var server = app.listen(5000, function () {
console.log('listening on port ', server.address().port)
})
Sachi.Dila
  • 1,126
  • 7
  • 15

1 Answers1

0

Try to use your mongoose schema's methods to fetch data simply with "find" and without population somehow simply like this:

router.get('/api/getdruginformation', function(req, res) {
  druginformation.find(function(err, data){
    if(err) throw err;
    res.send(data);
  });
});
JSEvgeny
  • 2,550
  • 1
  • 24
  • 38