Morgan Hayes Morgan Hayes - 3 months ago 7
AngularJS Question

Query first works but find does not

I am using angularjs and

parse.com
to query for three members with
spotlight = true
.

When i use first it will find one but when i use find with
limit(3)
it will find nothing. I have even removed the
Limit(3)
and still nothing. I have searched the internet, after trying a few things i found still result is zero.

app.controller('spotlightCtrl', function($scope, $q) {

$scope.sl = {};
$scope.slmemid = "";

Parse.initialize("xxx", "xxx");
Parse.serverURL = 'https://parseapi.back4app.com';
var ArticleDfd = $q.defer();
var members = Parse.Object.extend('members');
var query = new Parse.Query(members);
query.equalTo("spotlight", true);
query.first().then(function (data) {
//query.find().then(function (data) { -----this find does not return results.
ArticleDfd.resolve(data);
}, function (error) {
ArticleDfd.reject(data);
console.log(error);
});
ArticleDfd.promise
.then(function (article) {
$scope.sl = article.attributes;
$scope.slmemid = article.id;
console.log(article);
})
.catch(function (error) {
//do something with the error
console.log(error);
});

});


Still looking for a way to do this right.
I have found a work around. I use the skip() function and make three controllers.

app.controller('spotlightCtrl1', function($scope, $q) {.....
.....
query.equalTo("spotlight", true);
query.first().then(function (data) {

app.controller('spotlightCtrl2', function($scope, $q) {......
.....
query.equalTo("spotlight", true).skip(1);
query.first().then(function (data) {...

app.controller('spotlightCtrl3', function($scope, $q) {......
.....
query.equalTo("spotlight", true).skip(2);
query.first().then(function (data) {....


I think this will be slower. still want to know the right code??

Answer

After searching and asking questions to Stackoverflow and back4app.com I have found my own work around (all my questions received little feedback).

I used REST. There was a trickyness with the "greaterthan" (#gt) date. I the working syntax on stackoverflow. Also "Just remember to use https://parseapi.back4app.com/ endpoint instead of https://api.parse.com/1/" from Davi Macedo that i came across in a search on Google Groups.

$http({
      method: 'GET',
      url: ' https://parseapi.back4app.com/classes/Events',
      headers: {
        'X-Parse-Application-Id' : 'xxxx',
        'X-Parse-REST-API-Key' : 'xxxx',
        'Content-Type' : 'application/json'
      },
      params:  { 
                 where: {
                     'Luncheon': true,
                     'eventDate':{'$gt': {
                     "__type":"Date", "iso": currentTime
                        }
                     }
                 },
                 order: 'eventDate',
                 limit: 2
              }
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        $scope.events = response.data.results;

      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        console.log(response);
  });

This has worked well and my query completely works to replace the broken find query.

Comments