Rambo
Rambo

Reputation: 159

Unable to get Query.find() to return data, even though I know it exists

In my Parse server hosted on Heroku, I’m having difficulty making a query return data.

Here is my code:

Parse.Cloud.define("findUser", function(request, response) {
  console.log("starting")
  var userQuery = new Parse.Query(Parse.User);
  userQuery.useMasterKey = true;
  userQuery.equalTo("objectId","fZpDmQQEVt")
  userQuery.find().then( function(result) { 
      console.log("Inside then with result: " + result.length);
      foundUser = result; 
      if(foundUser.length != 0){
        //return a promise here?
        console.log("Found a user, user is: " + foundUser)
      } else {
        console.log("did not find a foundUser")
      }

      return foundUser
  })
  console.log("exiting")
});

And my output is this:

2019-08-11T18:18:34.885630+00:00 app[web.1]: starting
2019-08-11T18:18:34.887246+00:00 app[web.1]: exiting
2019-08-11T18:18:34.888799+00:00 app[web.1]: info: Ran cloud function findUser for user undefined with:
2019-08-11T18:18:34.888802+00:00 app[web.1]: Input: {}
2019-08-11T18:18:34.888804+00:00 app[web.1]: Result: undefined {"functionName":"findUser","params":{}}
2019-08-11T18:18:34.922537+00:00 app[web.1]: Inside then with result: 0
2019-08-11T18:18:34.922609+00:00 app[web.1]: did not find a foundUser

From the logs, it is clear that it is not finding the user and returning it. However, for the sake of this test, I have hardcoded in my personal objectId and I know that is correct. In my original code, when I did not find the user, I created it. It would never find a user and then always error out when trying to create the record because it already existed. Can you help me understand why this is not returning my user with the hard coded query?

Upvotes: 0

Views: 47

Answers (2)

nevzat alkan
nevzat alkan

Reputation: 1

return userQuery.find().then( function(result) { 

you should return the promise back form the findUser function. or make it async and wait for the inner find returns.

Upvotes: 0

Davi Macêdo
Davi Macêdo

Reputation: 2984

Try this:

Parse.Cloud.define("findUser", async request => {
  const userQuery = new Parse.Query(Parse.User);
  const foundUser = await userQuery.get('fZpDmQQEVt', { useMasterKey: true });
  console.log("Found a user, user is: " + foundUser);
  return foundUser;
});

Upvotes: 1

Related Questions