Skip to content

Parse.Relation fetching related object data #2850

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

Closed
rohit-romley opened this issue Oct 10, 2016 · 4 comments
Closed

Parse.Relation fetching related object data #2850

rohit-romley opened this issue Oct 10, 2016 · 4 comments

Comments

@rohit-romley
Copy link

http://stackoverflow.com/questions/39955488/parse-relation-fetching-related-object-data

I have a Class called commonWall and it is related to User class.

I have a column of type 'relation' to User in class commonWall named clubbedUsers.

I want to get the list of related users. What would be the appropriated code for that?

Below is my code.. but it is not working... it returns

{"_type":"Relation","className":"User"}
no object id for the relationship is returned..

var CommonWall = Parse.Object.extend("CommonWall");

    var q = new Parse.Query("CommonWall");
        q.include("clubbedUsers");
        q.limit(10);
        q.equalTo("heading","- 30%");
        q.find()
        .then((responseData) => {
            let listviewDataArray = JSON.parse(JSON.stringify(responseData));
            // console.warn(JSON.stringify(responseData));
            // console.warn(JSON.stringify(Parse.User.current().id));

            for (var i = listviewDataArray.length - 1; i >= 0; i--) {
              console.warn(JSON.stringify(responseData[i].relation("clubbedUsers")));
              responseData[i].relation("clubbedUsers").query().each()
              //if(userRelation !== "undefined")
              // userRelation.find()
              .then((responseData1) => {
                console.warn(JSON.stringify(responseData1));
              });
            }
          resolve(listviewDataArray);
          })
        .catch(function(error) {
            console.warn(error)
            reject({status: 'could not refresh wall!'});
          });
@flovilmart
Copy link
Contributor

Check the documentation about relations here:

http://parseplatform.github.io/docs/js/guide/#using-parse-relations

and about the each method
That would somewhat look like:

let query = new Parse.Query('CommonWall');
query.find().then(function(results) {
   var result = results[0];
   result.relation('clubbedUsers').query().each(function(relatedObject) {
      console.log(relatedObject);
   });
});

@haroonKhan-10p
Copy link

haroonKhan-10p commented Jan 31, 2018

@flovilmart can you please tell what is I am missing here:
const user = await Parse.User.logIn(req.params.email, req.params.password);
const userRoleQuery = user.relation(Parse.Role).query();
const role = await userRoleQuery.find() // role is undefined
res.success(role);

my relations and collections look like:
relations

collections

@akaipham
Copy link

@haroonKhan-10p Since user.relation(Parse.Role).query() returns only Parse.Query, you need to call .find() to fetch the object. Reference here:

http://parseplatform.org/Parse-SDK-JS/api/v1.11.1/Parse.Relation.html#query

@flovilmart
Copy link
Contributor

@haroonKhan-10p if you're attempting to get the roles for a particular user this is how you should make the query:

const query = new Parse.Query(Parse.Role)
query.equalTo('users', user)
const roles = await query.find();

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

4 participants