A regular mongoose find does not seem to send back documents as json. Eeach doc is my database is a simple object.
{
"_id" : ObjectId("57a170f0736435d829d97c17"),
"ip" : "74.208.147.41",
"domain" : "test.com",
"wp" : "4.4.2",
"host_name" : "Web03",
"hosted" : 1.0
}
I'm assuming it's because mongoose does not respond with json but an array of documents I believe. But how can I map these to send back json and eventually loop over in view with angular.
Here's my get route
router.get('/api/sites', function(req, res, next) {
Site.find({}, function(err, sites) {
if (err) {
next(err)
} else {
return res.json({sites: sites});
}
});
});
EDIT: It definitely seems to be the mongoose find query. If I pass in a value for the "ip" field to query by(ex: Site.find({ip: '74.208.147.41'})..., I get a response. When I pass no values in, I don't get a response.
Here's my model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var sitesEntrySchema = new Schema({
ip: {
type: String,
required: true,
trim: true
},
domain: {
type: String,
required: true,
trim: true
},
wp: {
type: String,
required: true,
trim: true
},
host_name: {
type: String,
required: true
},
hosted: {
type: String,
required: true
}
});
// make this available to our users in our Node applications
var Site = mongoose.model('Site', sitesEntrySchema, 'site');
module.exports = Site;
EDIT 8/10/16: Finally updated my version of mongoose and the find query worked.
EDIT 2/01/17: Explictly defining collection to use was the real issue. This thread helped me: Mongoose always returning an empty array NodeJS