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

Error When uid is Invalid (deepPopulate.ts) #26

Open
101sam opened this issue Jan 2, 2025 · 0 comments
Open

Error When uid is Invalid (deepPopulate.ts) #26

101sam opened this issue Jan 2, 2025 · 0 comments
Assignees

Comments

@101sam
Copy link

101sam commented Jan 2, 2025

Bug Report: Error When uid is Invalid (deepPopulate.ts)

Problem Description

When uid is invalid, the strapi.getModel(uid) call returns undefined, causing an error when attempting to access model.attributes in the following line:

const model = strapi.getModel(uid);
const attributes = Object.entries(model.attributes);

This results in the following error:

[2025-01-02 11:14:44.926] error: Cannot read properties of undefined (reading 'attributes')
TypeError: Cannot read properties of undefined (reading 'attributes')
    at getDeepPopulate (/path/to/your/project/dist/src/middlewares/deepPopulate.js:17:45)
    ...

Steps to Reproduce

  1. Enable public access to an API endpoint.
  2. Send a GET request to an invalid resource, e.g., GET ~/api/articles/RandomID.
  3. Observe the error in the server console.

Expected Behavior

If uid is invalid, the code should handle the case gracefully without throwing an error. Instead, it should return an empty object or an appropriate fallback response.

Proposed Solution

Add a check to validate the model before accessing its attributes. Update the code as follows:

const model = strapi.getModel(uid);

if (!model) {
  return {}; // Return an empty object if the model is undefined
}

const attributes = Object.entries(model.attributes);

Impact

This fix ensures that invalid uid values are handled safely, preventing server errors and improving the robustness of the middleware.

Additional Notes

  • This issue occurs in the middleware file deepPopulate.js at line 17.
  • Ensure that similar checks are implemented wherever strapi.getModel is used to prevent similar errors.

Screenshots

2025-01-02_11-32-32

Additional context

Add any other context about the request here.

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

2 participants