4

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??

Morgan Hayes
  • 321
  • 2
  • 25
  • I don't see any issue with your query... If you remove the equalTo condition you are getting results? – Ran Hassid Aug 07 '16 at 06:09
  • That is what I thought, removing the equalTo gives me the same result. I get results with first and no results with find? any other ideas? – Morgan Hayes Aug 07 '16 at 06:16
  • i dont know angularjs but **first** returns one Parse Object but **find** return list of parse objects (array - [data])... do you receive some error or just nothing gets returned? – Mazel Tov Aug 07 '16 at 08:03
  • I would also say, try and log the catch errors. Side note, the wrapping of ArticleDfd defer is not needed, simply do : `ArticleDfd = query.first/query.find` – cYrixmorten Aug 07 '16 at 09:31
  • the only reason the find() will not work and first() will work is due to an error. I guess it is maybe because one of the records in your collection is corrupted. Can you please try to run the find() query and add the error log (if you got an error) to the question? – Ran Hassid Aug 07 '16 at 11:56
  • thanks all, I have edited my code above with added console.log's . when i use find there is no logs from the error's. i do get array(0) from logging article. what else should I try? – Morgan Hayes Aug 07 '16 at 15:01

1 Answers1

0

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 found 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.

Morgan Hayes
  • 321
  • 2
  • 25