parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Option `select()` is ignored when using `find()`

Open tiavina-mika opened this issue 3 years ago • 5 comments
trafficstars

New Issue Checklist

Issue Description

I want to get only some fields using select() when querying multiple User, It worked if I query just one User (with first), but not with find(). Instead it returned all fields.

Steps to reproduce

const users = await new Parse.Query(Parse.User)
 .select('firstName')
 .find();

const usersJSON = users.map(user => user.toJSON());
console.log(usersJSON );

Actual Outcome

[
    {
        "username": "[email protected]",
        "email": "[email protected]",
        "lastName": "ULast",
        "firstName": "User3",
        "seen": false,
        "createdAt": "2022-08-09T06:52:01.791Z",
        "updatedAt": "2022-08-10T06:34:18.644Z",
        "isOnline": true,
        "ACL": {
            "*": {
                "read": true
            },
            "6CNboVpyma": {
                "read": true,
                "write": true
            }
        },
        "sessionToken": "r:1b9aaeaac68c7cb03cc660a606eeaefe",
        "objectId": "6CNboVpyma"
    },
    {
        "firstName": "User1",
        "createdAt": "2022-08-10T06:32:40.022Z",
        "updatedAt": "2022-08-10T06:32:40.022Z",
        "ACL": {
            "*": {
                "read": true
            },
            "W3LGz80lMv": {
                "read": true,
                "write": true
            }
        },
        "objectId": "W3LGz80lMv"
    }
]

Expected Outcome

[
    {
        "firstName": "User3",
        "createdAt": "2022-08-09T06:52:01.791Z",
        "updatedAt": "2022-08-10T06:34:18.644Z",
        "objectId": "6CNboVpyma"
    },
    {
        "firstName": "User1",
        "createdAt": "2022-08-10T06:32:40.022Z",
        "updatedAt": "2022-08-10T06:32:40.022Z"
        "objectId": "W3LGz80lMv"
    }
]

Environment

Server

  • Parse Server version: 5.2.4
  • Operating system: Microsoft Windows 10
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): Local

Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: 5
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): MongoDB Atlas

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): JavaScript
  • SDK version: 3.4.3

Logs

tiavina-mika avatar Aug 10 '22 08:08 tiavina-mika

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

A PR with a failing test is needed here. Similar tests already exists:

https://github.com/parse-community/parse-server/blob/4c29d4d23b67e4abaf25803fe71cae47ce1b5957/spec/ParseObject.spec.js#L1945-L1978

The basic test indeed only uses first(), so maybe you could add a simple test for find():

https://github.com/parse-community/parse-server/blob/e9d23830a3a34d7d0762f835bd918f5978ab0338/spec/ParseQuery.spec.js#L3184

mtrezza avatar Aug 11 '22 09:08 mtrezza

@tiavina-mika Do you have any update on this? Can we close this?

If we cannot reproduce this, maybe we should at least improve testing and add a test for find(), as I commented above.

mtrezza avatar Sep 17 '22 16:09 mtrezza

I think this is as expected. createdAt, ACL, updatedAt, objectId are always returned regardless of .select.

However, I wasn't able to replicate the issue of other keys coming through too. Here's my passing test:

it('can select on user class', async () => {
    const [bob] = await Promise.all([
      Parse.User.signUp('bob', 'password', { firstName: 'bob' , lastName: 'bill' }),
      Parse.User.signUp('bob2', 'password', { firstName: 'bob2', lastName: 'bill2' }),
    ]);
    const users = await new Parse.Query(Parse.User)
      .select('firstName')
      .find({ sessionToken: bob.get('sessionToken') });
    const allKeys = [
      ...new Set(
        users
          .map(user => Object.keys(user.toJSON()))
          .flat()
          .sort()
      ),
    ];
    expect(allKeys).toEqual(['ACL', 'createdAt', 'firstName', 'objectId', 'updatedAt']);
});

This test creates two users with firstName and lastName, and then attempts to run a query with select. After that, it creates a unique array (allKeys) with all the returned keys from both find objects.

In your case it could be that [email protected] is the currentUser, and the additional keys are showing up as it's reference internally/in memory is the same.

dblythy avatar Sep 18 '22 04:09 dblythy

To be closed if no further answer from @tiavina-mika due to unable to replicate.

mtrezza avatar Sep 18 '22 08:09 mtrezza

Closing due to not reproducible.

mtrezza avatar Sep 21 '22 21:09 mtrezza