Search code examples
javascriptparse-platform

How to retrieve rows in a for loop which has a key and value from parse-platform with javascript


I am already working on java with parse-platform and I want to work with javascript too. I want to give a column and a key and retrive all rows with a for loop. For example in the code below I get rows which have "key" value in the item column then I want use them in a for loop. How can i do that with javascript.

I have searched references and i found how to retrieve data but the code was take only 1 row. I did'nt find anything about how to get multiple rows

ParseQuery < ParseObject > query = ParseQuery.getQuery("query");
query.whereEqualTo("item", "key");
query.findInBackground(new FindCallback<ParseObject>() {
        @Override
public void done(List < ParseObject > objects, ParseException e) {

  if (e != null) {
    //for some errors
  } else {
    if (objects.size() > 0) {
      for (ParseObject object : objects) {
        //get datas which you want 
        object.getInt("key");
      }
    }
  }
}
});

This code is my java code actually and I want to do almost same thing in JS but I have trouble in for loop. I cant find how can i make for loop in JS with Parseobjects.

query = new Parse.Query("query");
query.equalTo("shift", "night");
query.first().then(function (person) {
  if (person) {
    console.log('object found successful with shift: ' + object.get("shift") + ' and id: ' + object.get("id"));
  } else {
    console.log("Nothing found, please try again");
  }
}).catch(function (error) {
  console.log("Error: " + error.code + " " + error.message);
});

I tried this code for retrieve all persons who works night and write its id. But problem is this code retrieve only one person. In the documentation it writes it will return only one person but i want all persons whp works night.


Solution

  • try this:

    const query = new Parse.Query("query");
    query.equalTo("shift", "night");
    query.find().then(function (persons) {
      if (persons.length > 0) {
        persons.forEach(function (person) {
          console.log('object found successful with shift: ' + person.get("shift") + ' and id: ' + person.id);
        });    
      } else {
        console.log("Nothing found, please try again");
      }
    }).catch(function (error) {
      console.log("Error: " + error.code + " " + error.message);
    });