Skip to content

How to prevent nested populating? #141

Open
@samislam

Description

@samislam

I have the following Model:

const userSchema = new mongoose.Schema({
   name: String,
   by: { type: mongoose.Schema.ObjectId, ref: 'User' }
})
const UserModel = mongoose.model('User', userSchema, 'users')

and I am using api-query-params in one of my controllers:

const aqp = require('api-query-params')
app.get('/users', (req, res, next) => {
   const docs = await UserModel.find(aqp.filter).populate(aqp.population)
   res.status(200).json({  data: docs })
})

Let's say there's a user called A who created himself somehow.
I am requesting: http://localhost:4000/users?populate=by and I am getting the response as follows:

{ # 200 OK
   data: [
      {
          name: A,
          by: {
              name: A,
              by: 62e7960ebc9aa651523bf5b2 // Object Id
          }
      }
   ]
}

I am OK with this response, however, the problem comes when someone requests http://localhost:4000/users?populate=by.by.by.by

the response becomes:

{
    # 200 OK
    data: [{
        name: A,
        by: {
            name: A,
            by: {
                name: A,
                by: {
                    name: A,
                    by: 62e7960ebc9aa651523bf5b2 // Object ID
                }
            }
        }
    }]
}

Is there an option where I can only allow one level of populating in aqp (api-query-params)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions