Note that there are some explanatory texts on larger screens.

plurals
  1. POHide embedded document in mongoose/node REST server
    primarykey
    data
    text
    <p>I'm trying to hide certain fields on my GET output for my REST server. I have 2 schema's, both have a field to embed related data from eachother into the GET, so getting /people would return a list of locations they work at and getting a list of locations returns who works there. Doing that, however, will add a person.locations.employees field and will then list out the employees again, which obviously I don't want. So how do I remove that field from the output before displaying it? Thanks all, let me know if you need any more information.</p> <pre><code>/******************** / GET :endpoint ********************/ app.get('/:endpoint', function (req, res) { var endpoint = req.params.endpoint; // Select model based on endpoint, otherwise throw err if( endpoint == 'people' ){ model = PeopleModel.find().populate('locations'); } else if( endpoint == 'locations' ){ model = LocationsModel.find().populate('employees'); } else { return res.send(404, { erorr: "That resource doesn't exist" }); } // Display the results return model.exec(function (err, obj) { if (!err) { return res.send(obj); } else { return res.send(err); } }); }); </code></pre> <p>Here is my GET logic. So I've been trying to use the query functions in mongoose after the populate function to try and filter out those references. Here are my two schema's.</p> <p>peopleSchema.js</p> <pre><code>return new Schema({ first_name: String, last_name: String, address: {}, image: String, job_title: String, created_at: { type: Date, default: Date.now }, active_until: { type: Date, default: null }, hourly_wage: Number, locations: [{ type: Schema.ObjectId, ref: 'Locations' }], employee_number: Number }, { collection: 'people' }); </code></pre> <p>locationsSchema.js</p> <pre><code>return new Schema({ title: String, address: {}, current_manager: String, // Inherit person details alternate_contact: String, // Inherit person details hours: {}, employees: [{ type: Schema.ObjectId, ref: 'People' }], // mixin employees that work at this location created_at: { type: Date, default: Date.now }, active_until: { type: Date, default: null } }, { collection: 'locations' }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload