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

Custom API call does not filter all columns #1250

Closed
piere129 opened this issue May 22, 2018 · 6 comments
Closed

Custom API call does not filter all columns #1250

piere129 opened this issue May 22, 2018 · 6 comments
Assignees
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve

Comments

@piere129
Copy link

Informations

  • Node.js version: 9.9.0
  • npm version: 5.6.0
  • Strapi version: 3.0.0-alpha.12.2
  • Database: MongoDB
  • Operating system: Windows 10

What is the current behavior?

I'm calling a custom API route called /client/categories/:code ,that should return the categories column based on the entered ":code" passed. In service:

findCategoriesUser: (params) => {
        var query = Client.findOne({'access_code': params.code});
        query.select({  "_id":0, "avatar_image":0, "id":0, "access_code":0, "createdAt":0, "updatedAt":0, "__v":0, "products":0});
        return query.exec();
    },

The API call works but for some reason, the payload returned still includes the fields "avatar_image", "products" and "id", on top of the categories field:

{
    "categories": [
        "5b0412434af3412c685af19e"
    ],
    "products": null,
    "avatar_image": null,
    "id": null
}

Steps to reproduce the problem

make custom routing for calling the service and set all columns except the 'categories' column to 0.

What is the expected behavior?

API result should not include "avatar_image", "products" and "id"

Suggested solutions

I'm guessing there is a faulty hook somewhere that causes this

@lauriejim lauriejim self-assigned this May 22, 2018
@lauriejim lauriejim added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around labels May 22, 2018
@lauriejim
Copy link
Contributor

Hi @piere129 , thank you for this report. Can you check the request log you receive in your mongoDB ?

@piere129
Copy link
Author

the api call does a total of 3 requests, 2 of them being user-permission requests which don't seem to be the problem. The third one gives me the following JSON when logging the request:

{
    "op" : "query",
    "ns" : "securitas.client",
    "query" : {
        "find" : "client",
        "filter" : {
            "access_code" : "ABC-123"
        },
        "projection" : {
            "_id" : 0,
            "avatar_image" : 0,
            "access_code" : 0,
            "createdAt" : 0,
            "updatedAt" : 0,
            "__v" : 0,
            "products" : 0,
            "id" : 0
        },
        "limit" : 1,
        "singleBatch" : true,
        "batchSize" : 1,
        "returnKey" : false,
        "showRecordId" : false
    },
    "keysExamined" : 1,
    "docsExamined" : 1,
    "cursorExhausted" : true,
    "keyUpdates" : 0,
    "writeConflicts" : 0,
    "numYield" : 0,
    "locks" : {
        "Global" : {
            "acquireCount" : {
                "r" : NumberLong(2)
            }
        },
        "Database" : {
            "acquireCount" : {
                "r" : NumberLong(1)
            }
        },
        "Collection" : {
            "acquireCount" : {
                "r" : NumberLong(1)
            }
        }
    },
    "nreturned" : 1,
    "responseLength" : 177,
    "protocol" : "op_query",
    "millis" : 0,
    "execStats" : {
        "stage" : "LIMIT",
        "nReturned" : 1,
        "executionTimeMillisEstimate" : 0,
        "works" : 1,
        "advanced" : 1,
        "needTime" : 0,
        "needYield" : 0,
        "saveState" : 0,
        "restoreState" : 0,
        "isEOF" : 1,
        "invalidates" : 0,
        "limitAmount" : 1,
        "inputStage" : {
            "stage" : "PROJECTION",
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 1,
            "advanced" : 1,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 0,
            "invalidates" : 0,
            "transformBy" : {
                "_id" : 0,
                "avatar_image" : 0,
                "access_code" : 0,
                "createdAt" : 0,
                "updatedAt" : 0,
                "__v" : 0,
                "products" : 0,
                "id" : 0
            },
            "inputStage" : {
                "stage" : "FETCH",
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 1,
                "advanced" : 1,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 0,
                "invalidates" : 0,
                "docsExamined" : 1,
                "alreadyHasObj" : 0,
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "nReturned" : 1,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 1,
                    "advanced" : 1,
                    "needTime" : 0,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 0,
                    "invalidates" : 0,
                    "keyPattern" : {
                        "access_code" : 1
                    },
                    "indexName" : "access_code_1",
                    "isMultiKey" : false,
                    "isUnique" : true,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 1,
                    "direction" : "forward",
                    "indexBounds" : {
                        "access_code" : [ 
                            "[\"ABC-123\", \"ABC-123\"]"
                        ]
                    },
                    "keysExamined" : 1,
                    "dupsTested" : 0,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0
                }
            }
        }
    },
    "ts" : ISODate("2018-05-23T06:42:26.723Z"),
    "client" : "127.0.0.1",
    "allUsers" : [],
    "user" : ""
}

@piere129
Copy link
Author

piere129 commented May 23, 2018

I also just noticed that populating a list of categories, does not include the image details of those filled categories, but instead returns null, is this intentional..? And if so, how can this be worked around?

@lauriejim
Copy link
Contributor

@piere129 I have no time to test it. But the request you did with .select() is something that work with standalone Mongoose instance. Or this issue come from Strapi mongoose intégration.

And if you don't use .select() the data in well get ?

@piere129
Copy link
Author

piere129 commented May 23, 2018

@lauriejim If i don't use the select it just returns all data as if you'd call a default api call. But that's not what I'm trying to achieve. I believe there is a feature in Strapi that adds these variables, after i do my mongoose query, a hook perhaps?

@lauriejim lauriejim added severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve issue: enhancement Issue suggesting an enhancement to an existing feature and removed severity: medium If it breaks the basic use of the product but can be worked around issue: bug Issue reporting a bug labels Jun 5, 2018
@lauriejim
Copy link
Contributor

Please try. It work on my side

await Client
  .findOne({
    code: "eee"
  }).select({name: 1});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve
Projects
None yet
Development

No branches or pull requests

2 participants