0

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

Community
  • 1
  • 1
Calrocks
  • 65
  • 1
  • 4
  • 11
  • 1
    Possible duplicate. Maybe this will help. http://stackoverflow.com/questions/12210870/how-to-get-array-of-json-objects-rather-than-mongoose-documents – Mykola Borysyuk Aug 06 '16 at 21:09
  • @MykolaBorysyuk thanks! I've seen this but after enabling mongoose errors to print to the console. I see this; sites.find({}) { fields: undefined } so I think mongoose is not even executing querying. – Calrocks Aug 06 '16 at 21:20
  • @Calrocks That's the expected debug output when you don't select specific fields. – JohnnyHK Aug 07 '16 at 14:17
  • @JohnnyHK oh ok. Thank you. That's good to know. Any idea why I don't get a response failed request when I hit the above get route? – Calrocks Aug 08 '16 at 03:31
  • @Calrocks It's not clear from you question what's not working. Can you [edit] your question to provide more details? – JohnnyHK Aug 08 '16 at 14:51
  • @JohnnyHK made an edit. def seems to be mongoose query. – Calrocks Aug 10 '16 at 03:51
  • @Calrocks Hmm...that doesn't really make sense. What does `console.log(sites)` show in the "query all" case? – JohnnyHK Aug 10 '16 at 04:11
  • @JohnnyHK exactly. Finally decided to update my version of mongoose to the latest and query suddenly worked! Thanks so much for taking the time to try and help tho. Cheers! – Calrocks Aug 10 '16 at 15:25

1 Answers1

0
router.get("/api/sites", async function(req, res, next) {
  try {
    const sites = await Site.find();
    res.json({ sites });
  } catch (e) {
    next(e);
  }
});

https://mongoosejs.com/docs/api/model.html#model_Model-find

The documentation says .find() takes 3 parameters and it doesn't mention a callback function to be one of its parameters(doesn't take callback function). And the above code worked fine for me.