Parse.com JS在使用query.find()时循环遍历数组

时间:2016-01-13 20:33:16

标签: javascript arrays loops parse-platform

我试图使用数组中的每个元素作为约束进行查询,但到目前为止它只给了我一个结果。

这是数组:

["C44","C43","C45","C117"]

我在这里:

{var TestObject = Parse.Object.extend("TestObject");
    var query = new Parse.Query(TestObject);
    query.get("W7yuUsbbav", {
        success: function(testObject) {
            var subjectArray = testObject.get("subjects");


            for (i = 0; i < subjectArray.length ; i++ ){

                console.log(subjectArray[i]);
                var query = new Parse.Query("Subjects");
                query.equalTo("subCode", subjectArray[i]);
                query.find({
                    success: function(results) {
                        $scope.$apply(function() {
                            $scope.subjects1 = results.map(function(obj) {
                                return {subCode: obj.get("subCode"), subName: obj.get("subName"),  subDesc: obj.get("subDesc")};
                            });
                        });
                    },
                    error: function(error) {
                    }
                });
            }

        },
        error: function(object, error) {
            // The object was not retrieved successfully.
            // error is a Parse.Error with an error code and message.
        }
    });}

1 个答案:

答案 0 :(得分:1)

您可以使用query的containsIn属性。

var sampleId = "W7yuUsbbav";
var query1 = new Parse.Query("TestObject");
query1.get(sampleId).then(function (testObject) {

    // Success
    var subjectArray = testObject.get("subjects");

    var query2 = new Parse.Query("Subjects");
    query2.containedIn("subCode", subjectArray);

    return query2.find();

}).then(function (subjectObjects) {

    // Success
    // results is an array of objects

}, function (error) {

    // Error

});