1

I'm running a Parse Query on my User class.

I want to retrieve users that are contained in an array of strings (ID).

const usersQuery = new Parse.Query(User).containedIn('objectId', customersArrayId).find({ useMasterKey: true });

Which works, actually. I'm getting a [ParseUser { _objCount: 6, className: '_User', id: 'iYIJ7Zrmms' }], because only 1 user matches.

But well, my issue is that I'm only getting ParseUser { _objCount: 6, className: '_User', id: 'iYIJ7Zrmms' }. This class contains other fields (firstname, lastname, e.g.) that are not returned.

When I performed the same thing, looping on my customersArrayId and performing .get():

const customerId = customersArrayId[index];
promises.push(new Parse.Query(User).get(customerId).then((user) => { 
  return user.toJSON();
}, (error) => console.error(error)));

I'm getting the full object, as expected. But it doesn't seem to be the right way of querying Parse objects from an array of ids.

I can't find anything in the docs about it, any idea why containedIn only returns a part of the queried objects?

1 Answer 1

0

I actually get the difference:

new Parse.Query(User).get(customerId)

=> returns the Parse Object

new Parse.Query(User).containedIn('objectId', customersArrayId)

=> returns the Parse User, subclass of a Parse Object.

And well, then, this thread was useful: Get user from parse in Javascript

I ended up using :

usersQuery.then(customersResponse => {
    const customers = [];
    for (let index = 0; index < customersResponse.length; index++) {
      const customer = {
        ...customersResponse[index].toJSON(),
        ...customersResponse[index].attributes
      }; 
      ...

Still not sure that the best answer.

EDIT

router.get('/:userId/shop/customers/details', checkUserMatch, (req, res, next) => {


if('shops' in req.jwtData.data) {
    const shopId = req.jwtData.data.shops[0];
    const query = new Parse.Query('OtherStuff');
    const Shop = Parse.Object.extend('Shops');
    query.equalTo('shop', new Shop({id: shopId})).find().then((otherStuffs) => {
      const customersArrayId = otherStuffs.map(otherStuff => otherStuff.toJSON().user.objectId);
      const usersQuery = new Parse.Query('_User').containedIn('objectId', customersArrayId).find({ useMasterKey: true });
      usersQuery.then(customersResponse => {
        const customers = [];
        for (let index = 0; index < customersResponse.length; index++) {
          let customer = {
            ...customersResponse[index].toJSON(),
            ...customersResponse[index].attributes
          }; 
          const customerId = customer.objectId;         
          const stuffQuery = new Parse.Query('Stuff').equalTo('user', new UserModel({objectId: customerId})).find().then((stuff) => {
            return stuff;
          });
          const otherStuffQuery = new Parse.Query('otherStuff').equalTo('user', new UserModel({objectId: customerId})).find().then((otherStuff) => {
            return otherStuff;
          });
          Promise.all([stuffQuery, otherStuffQuery]).then((data) => {
            const stuff = data[0];
            const otherStuff = data[1];
            customer = {
              ...customer,
              stuff,
              otherStuff,
            }
            customers.push(customer);
            if(index === customersResponse.length - 1) { // last customer
              res.json({
                success: true,
                data: customers
              });
            }
          })
        }


      });

    });
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Your last code looks good. Can you share the complete version so I can better advise?
I updated my answer, glad someone from B4A is looking at this, since I'm actually using B4A on this project :)
Nice! Your code looks good to me and it should work properly. But you can probably improve it using relational queries. Take a look at matchesQuery and matchesKeyInQuery constraints.
Thanks for the tip, I'll try that and let you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.