Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent nested populating? #141

Open
samislam opened this issue Aug 1, 2022 · 0 comments
Open

How to prevent nested populating? #141

samislam opened this issue Aug 1, 2022 · 0 comments

Comments

@samislam
Copy link

samislam commented Aug 1, 2022

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant