Search code examples
parse-platform

Parse server query object is returning no results


I am having issues with querying the parse server. I have created a class called Tickets and a single row via the Dashboard. When I use curl, I can query and get the row. However, when I try to do it in JavaScript from an HTML page, I get zero results.

I call the getTickets() function from myLogin, which is called via a button click. The final console.log() call in getTickets() prints "Results: " and nothing else.

BTW, the getTickets() function is modeled on examples in the Parse documentation.

Thanks in advance for your help.

Here is my code:

    Parse.initialize("-REDACTED-", "");
    Parse.serverURL = "-REDACTED-";

    function myLogin(uname, psw) {
      var uname = document.getElementById("uname").value;
      var psw = document.getElementById("psw").value;
      const user = Parse.User.logIn(uname, psw);
      var cUser = Parse.User.current();

      if (cUser) {
        console.log(
          "Hey! The login worked!" + " Username: " + cUser.get("username")
        );
        getTickets();
      } else {
        alert("Womp, womp. The login failed.");
      }
    }

    async function getTickets() {
      const Ticket = Parse.Object.extend("Ticket");
      const query = new Parse.Query(Ticket);
      const res = await query.find();
      console.log("Results: " + res);
    }

The curl call returns the following, if that's useful:

{"results":[{"objectId":"RuFPxSjigL","user":{"__type":"Pointer","className":"_User","objectId":"uGwwN99EX9"},"createdAt":"2020-01-15T20:47:45.116Z","updatedAt":"2020-01-15T20:48:05.203Z","event":{"__type":"Pointer","className":"Event","objectId":"BdGQSPhK0J"}}]}%

Solution

  • There are minor things I'd change:

    Parse.initialize("imAppId", "");
    Parse.serverURL = "https://www.immedia.co/parse/";
    
    async function imLogin(uname, psw) {
      const uname = document.getElementById("uname").value;
      const psw = document.getElementById("psw").value;
      let user;
      try {
        user = await Parse.User.logIn(uname, psw);
      } catch (err) {
        alert("Login failed: username or password are not valid: ", err);
        return; // if log in fails, you can't access the user's tickets
      }
    
      let tix;
      try {
        tix = await getTickets();
        alert("Successfully retrieved " + tix.length + " tickets.");
      } catch (err) {
        console.error("Error getting tickets: ", err);
        return;
      }
    
      for (const obj of tix) {
        console.log(
          "ticket.id: " + obj.id + " with user.id " + obj.get("user")
        );
      }
    }
    
    async function getTickets() {
      const Ticket = Parse.Object.extend("Ticket");
      const query = new Parse.Query(Ticket);
      return await query.find();
    }
    

    You can convert the for loop to a for-of loop. I'd not catch an error in getTickets, but in the log in function. And if log in or get tickets fails, return after logging the error, as there's nothing more to do.