2

Background:

I have a Parse database of images. Simply, my code does this:

A user, through a Parse Cloud call requests an image ("getNewPicture"). Nested within I check if he has seen any pictures before (alongside other requirements) and if so deliver one specific picture (getSpecifiedPicture). If he has not, then I deliver a new picture (getNewPicture).

Issue:

Calling "getNewPicture" through Parse Cloud Code function I get an error code 141. What's strange is that it works through Android but not iOS.

My code:

Parse.Cloud.define("getNewPicture", function(request, response) {
  var SeenPictures = Parse.Object.extend("SeenPictures");
  var query = new Parse.Query(SeenPictures);
  var username = request.params.username;
  var notWantedPics = [];
  query.ascending("createdAt");
  query.equalTo("username", username);
  query.find({
    success: function(results) {
      for (var i = 0; i < results.length; i++) {
        if (results[i].get("likes") == 1 || results[i].get("dislikes") == 1) {
          notWantedPics.push(results[i].get("pictureId"));
          results.splice(i, 1);
          i--;
        }
      }
      if (results != 0) {
        getSpecifiedPicture(results[0].get("pictureId"), {
          success: function(returnValue) {
            response.success(returnValue);
          },
          error: function(error) {
            response.error(error);
          }
        });
      } else {
        getNewPicture(username, notWantedPics, {
          success: function(returnValue) {
            response.success(returnValue);
          },
          error: function(error) {
            response.error(error);
          }
        });  
      }
    },
    error: function() {
      response.error(error);
    }
  });
});

function getSpecifiedPicture(specifiedPic, callback) {
  var Pictures = Parse.Object.extend("Pictures");
  var pictures = new Parse.Query(Pictures);
  pictures.get(specifiedPic, {
    success: function(picture) {
      callback.success(picture);
    },
    error: function(error) {
      callback.error(error);
    }
  });
}

function getNewPicture(username, notWantedPics, callback) {
  var Pictures = Parse.Object.extend("Pictures");
  var pictures = new Parse.Query(Pictures);
  pictures.notEqualTo("photographerUserName", username);
  pictures.notContainedIn("objectId", notWantedPics);
  pictures.ascending("createdAt");
  pictures.find({
    success: function(results) {
      if (results.length > 0) {
        var object = results[0];
        //Some other fancy stuff
        object.save();
        callback.success(object);
      }
    },
    error: function(error) {
      callback.error(error);
    }
  });
}

Why am I getting code 141? Any help is appreciated.

Thanks.

1 Answer 1

2

Your callbacks are a mess. I rewrote it to follow more of a promise chain style. Much easier to follow. Also, underscore.js is your friend. Hopefully I got your idea right.

var _ = require('underscore'); // Javascript Library

Parse.Cloud.define("getNewPicture", function(request, response) {

    var username = request.params.username;
    var notWantedPics = [];

    if (!username) {
        return response.error('No username.');
    }

    var query1 = new Parse.Query("SeenPictures");
    query1.ascending("createdAt");
    query1.equalTo("username", username);
    var SeenPictures = query1.find();

    return Parse.Promise.when([SeenPictures]).then(function (SeenPictures) {

        SeenPictures = _.filter(SeenPictures, function (SeenPicture) {
            if (SeenPicture.get("likes") == 1 || SeenPicture.get("dislikes") == 1) {
                notWantedPics.push(SeenPicture.get("pictureId"));
                return false;
            } 
            else {
                return true;
            }
        });

        // notWantedPics?

        if (SeenPictures > 0) {
            var query2 = new Parse.Query("Pictures");
            var Pictures = [query2.get(SeenPictures[0].get('pictureId'))];
        } 
        else {
            var query2 = new Parse.Query("Pictures");
            query2.notEqualTo("photographerUserName", username);
            query2.notContainedIn("objectId", notWantedPics);
            query2.ascending("createdAt");
            var Pictures = query2.find();
        }

        return Parse.Promise.when([Pictures]);

    }).then(function (Pictures) {

        if (Pictures > 0) {

            // Success
            return response.success(Pictures[0]);

        } else {
            return Parse.Promise.error("No pictures.");
        }

    }, function (error) {
        // Error
        return response.error(error);
    });

});
Sign up to request clarification or add additional context in comments.

1 Comment

No problem! BTW, hi from Ottawa!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.