ai20 ai20 - 4 months ago 13
AngularJS Question

Javascript angular parse queries syncronously (in sequence)

I am trying to use Parse in javascript to make two queries syncrounsly in sequence:

var innerQuery = new Parse.Query(Attendees);
innerQuery.equalTo("user_id",$localStorage.username);
innerQuery.equalTo("status","confirmed")
innerQuery.equalTo("code_verified",false)

var innerQuerytrue = new Parse.Query(Attendees);
innerQuerytrue.equalTo("user_id",$localStorage.username);
innerQuerytrue.equalTo("status","confirmed")
innerQuerytrue.equalTo("code_verified",true)


innerQuery.find({})
innerQuerytrue.find({})


var eventDetails = []

var queryfalse = new Parse.Query(Events)
queryfalse.matchesKeyInQuery("id_event","event_id",innerQuery);

var querytrue = new Parse.Query(Events)
querytrue.matchesKeyInQuery("id_event","event_id",innerQuerytrue);


queryfalse.find().then(function(results){
for (var i in results) {
var object = results[i];
var eventId = object.get("id_event");
var eventname = object.get("event_name");
var datestart = object.get("date_start");
var location = object.get("location");
var eventimagefile = object.get("event_image");
var eventimageurl = eventimagefile.url();
eventDetails.push({'name':eventname,'eventId':eventId, 'location':location, 'datestart':datestart, 'eventphoto':eventimageurl,'verified':false})
}

}).then(function(){
querytrue.find().then(function(results1){
for (var i in results1) {
var object = results1[i];
var eventId = object.get("id_event");
var eventname = object.get("event_name");
var datestart = object.get("date_start");
var location = object.get("location");
var eventimagefile = object.get("event_image");
var eventimageurl = eventimagefile.url();
eventDetails.push({'name':eventname,'eventId':eventId, 'location':location, 'datestart':datestart, 'eventphoto':eventimageurl,'verified':true})
}
})

}).then(function(){
$scope.events = eventDetails;
})


The second query, queryfalse, is not always executed. I am using promises and not sure if it is the right way to use them. Any idea?

Answer

You have to move the angular result to the 2nd promise.

var innerQuery = new Parse.Query(Attendees);
    innerQuery.equalTo("user_id",$localStorage.username);
    innerQuery.equalTo("status","confirmed")
    innerQuery.equalTo("code_verified",false)

    var innerQuerytrue = new Parse.Query(Attendees);
    innerQuerytrue.equalTo("user_id",$localStorage.username);
    innerQuerytrue.equalTo("status","confirmed")
    innerQuerytrue.equalTo("code_verified",true)


    innerQuery.find({}) 
    innerQuerytrue.find({}) 


    var eventDetails = []

    var queryfalse = new Parse.Query(Events)
    queryfalse.matchesKeyInQuery("id_event","event_id",innerQuery);

    var querytrue = new Parse.Query(Events)
    querytrue.matchesKeyInQuery("id_event","event_id",innerQuerytrue);


    queryfalse.find().then(function(results){
        for (var i in results) {
              var object = results[i];
              var eventId = object.get("id_event");
              var eventname = object.get("event_name");
              var datestart = object.get("date_start");
              var location = object.get("location");
              var eventimagefile = object.get("event_image");
              var eventimageurl = eventimagefile.url();
              eventDetails.push({'name':eventname,'eventId':eventId, 'location':location, 'datestart':datestart, 'eventphoto':eventimageurl,'verified':false})      
        }

    }).then(function(){
        querytrue.find().then(function(results1){
            for (var i in results1) {
                  var object = results1[i];
                  var eventId = object.get("id_event");
                  var eventname = object.get("event_name");
                  var datestart = object.get("date_start");
                  var location = object.get("location");
                  var eventimagefile = object.get("event_image");
                  var eventimageurl = eventimagefile.url();
                  eventDetails.push({'name':eventname,'eventId':eventId, 'location':location, 'datestart':datestart, 'eventphoto':eventimageurl,'verified':true})      
            }
        }).then(function(){
            $scope.events = eventDetails;
        });
    })
Comments