2

I am trying to retrieve data from the Database of Parse Server and I am facing a 500 Internal Error.

My class is "Friends" and the column is "fromUser", which is a Pointer to <_User> class.

The point is to get the usernames from the column "username" which is inside of the "User" Class.

I develop the app with Ionic 3 and trying to run the function with a current user logged in to retrieve the data.

My cloud code:

Parse.Cloud.define('FriendsQuery', function(req,res) {
const query = new Parse.Query("Friends");
query.equalTo("fromUser", request.params.currentuser);
query.find().then((results) => {
    res.success(results);
})
.catch(() =>  {
  response.error("user lookup failed");
});
});

and my typescript code

currentUser = Parse.User.current();
retrieveFriends(){
if (this.currentUser= Parse.User.current()) {
  const passdata = Parse.User.current();
  Parse.Cloud.run('FriendsQuery', {currentuser: this.currentUser }).then(
  res => {
    console.log(res)
    }
  )
}

}

Any ideas?

giorgionasis
  • 394
  • 1
  • 7
  • 17

2 Answers2

0

for parse cloud code objects not allowed so you have to send only the user id and do a query in cloud to get the user object like the following

typescript code:

    currentUser = Parse.User.current();
retrieveFriends(){
if (this.currentUser= Parse.User.current()) {
  const passdata = Parse.User.current();
  Parse.Cloud.run('FriendsQuery', {currentuser: this.currentUser.id }).then(
  res => {
    console.log(res)
    }
  )
}
}

cloud code:

    Parse.Cloud.define('FriendsQuery', function(req,res) {
const query = new Parse.Query("Friends");
var currentId =  req.params.currentuser;
var currentuserobject = {};
 var currentuserquery = new Parse.Query(Parse.User);
    currentuserquery.equalTo("objectId", currentId);  // find all the women
    currentuserquery.first({
      success: function(user) {
        user = currentuserobject;
      }
    });
query.equalTo("fromUser", currentuserobject);
query.find().then((results) => {
    res.success(results);
})
.catch(() =>  {
  response.error("user lookup failed");
});
});
  • Hi, Marcos and thank you. Console log returnes an empty array.I attach image. I have edited the question to be more clear. [link] (https://imgur.com/a/t7c6v) – giorgionasis Oct 10 '17 at 04:39
0

so please try this

Parse.Cloud.define('FriendsQuery', function(req,res) {
var currentId =  req.params.currentuser;
var currentuserquery = new Parse.Query(Parse.User);
    currentuserquery.equalTo("objectId", currentId);  // find all the women
    currentuserquery.first({
      success: function(user) {
      const query = new Parse.Query("Friends");
      query.equalTo("fromUser", user);
      query.find().then((results) => {
      res.success(results);
      })

      }
    })

.catch(() =>  {
  res.error("user lookup failed");
});
});
  • Hi Marcos, it seems to work somehow, but it returnes in console log only one object not the array, which I need. Also, with "first" it returns only one result. However, with this function I am able to pass the current user id: retrieveFriends(){ if (this.currentUser= Parse.User.current()) { console.log(this.currentUser.id) Parse.Cloud.run('FriendsQuery', {currentuser: this.currentUser.id}).then( res => { console.log(res) }); } } So I can send to the server the id of the user, but I need as response the array, not the object – giorgionasis Oct 13 '17 at 08:11
  • this should return an array of objects unless there is only one object in the database class friends please send screenshoot of your parse dashboard – Marcos T. Gobriel Oct 13 '17 at 08:35
  • this is the dashboard.maybe my concept of query is wrong. I want to retrieve the list of friends of a user. So when i query toUser or from User, I want as result the other user. [link] (https://imgur.com/a/L3BGO) – giorgionasis Oct 13 '17 at 12:48
  • yes it is better to add a relation column instead of pointer and this relation would contain all ids of friends – Marcos T. Gobriel Oct 13 '17 at 13:19
  • you mean to add an extra column to Users class where it could be an array of friends?with their ids? – giorgionasis Oct 13 '17 at 13:23
  • Actually it will be relation which point to user class not array it will be easier to read and add objects to it – Marcos T. Gobriel Oct 13 '17 at 13:53
  • Thank you for your interest Marcos, but unfortunately I cant get it..I opened a new question about this [link] (https://stackoverflow.com/questions/46731044/parse-cloude-right-query-to-retrieve-friendslist-and-not) – giorgionasis Oct 13 '17 at 14:00