Udit Udit - 3 months ago 10
Node.js Question

express server api returns empty

i am trying to get the data from mongodb using express server but all i am getting is empty array => []

However if i run the db.Goserv.find() in console i get the results right please help

here is the server.js file

var Schema = mongoose.Schema;

var schema = new Schema({

type: String,
address: String,

servicecost: String

}, { collection: 'Goserv' });





var Goserv = mongoose.model('Goserv', schema );

module.exports = Goserv ;


app.get('/api/centre', function(req, res) {


Goserv.find(function(err, centre){
if(err){
res.send(err);
} else {
res.json(centre);
console.log(centre);
}

});

});

Answer

Try this...

  var Schema = mongoose.Schema;

    var schema = new Schema({

    type: String,
    address: String,

    servicecost: String

}, { collection: 'Goserv' });





var Goserv = mongoose.model('Goserv', schema  );

 module.exports = Goserv ;


    app.get('/api/centre', function(req, res) {


        Goserv.find({},function(err, centre){
            if(err){
                res.send(err);
           } else {
                res.json(centre);
                console.log(centre);
           }

        });

    });