阅读背景:

Parse Cloud Code嵌套查询的代码141(错误:未成功/错误调用)

来源:互联网 

Background:

背景:

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

我有一个Parse的图像数据库。简单地说,我的代码是这样的:

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

用户通过Parse Cloud调用请求图像(“getNewPicture”)。嵌套在我内部检查他之前是否看过任何照片(以及其他要求),如果是,则提供一张特定图片(getSpecifiedPicture)。如果他没有,那么我发送一张新照片(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.

通过Parse Cloud Code函数调用“getNewPicture”我得到一个错误代码141.奇怪的是它可以通过Android而不是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.

我为什么要获得代码141?任何帮助表示赞赏。

Thanks.

谢谢。

1 个解决方案

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

你的回调是一团糟。我重新编写它以遵循更多的承诺链样式。更容易遵循。另外,underscore.js是你的朋友。希望我的想法是正确的。

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);
    });

});

分享到: