Im new to nodejs.i already have a db named aqi with collection name pln. Im trying to show all the records in collection on web page but mongoose always returns empty array. i have tested it with other dbs but i can get data from them but for pln mongoose always return empty array. i would really appreciate it if someone could help me out. this is my schema
var Pln = new Schema({
latit : Number,
longit : Number,
timestmp : String,
co : Number,
smoke : Number,
O3 : Number,
humidity : Number,
temperature: Number,
co2 : Number,
dust : String
});
var plnModel = mongoose.model('pln', Pln);
This is my route.
app.get('/', function(req, res) {
res.contentType('application/json');
plnModel.find({}, function(err, pln) {
if (pln != null) {
console.log('Found the User:' + pln.latit);
res.send(JSON.stringify(pln));
}
});
});
plnin an array, so testing forif (pln != null)is useless as this will always be true. On the other hand,pln.latitwill always be undefined because[].latitis undefined. Also, why are you stringifying the documents? It can't work, because you setcontentType('application/json')and you're sending back a string. Just send backres.status(200).json(pln).