2015-04-05 45 views
1

我在JavaScript中使用Parse查詢時遇到問題。我想嘗試使用多個doesNotMatchKeyInQuery函數,但它只允許使用最後一個函數。任何想法,我可以如何使代碼這樣的工作?忽略代碼其他部分可能存在的錯誤。我以此爲例解析多個doesNotMatchKeyInQuery

//Query 1 
var Class1 = Parse.Object.extend("Class1"); 
var class1Query = new Parse.Query(Class1); 
class1Query.equalTo("id", id1); 

//Query 2 
var Class2 = Parse.Object.extend("Class2"); 
var class2Query = new Parse.Query(Class2); 
class2Query.equalTo("id", id2); 

//Query 3 
var Class3 = Parse.Object.extend("Class3"); 
var class3Query = new Parse.Query(Class3); 
class3Query.equalTo("id", id3); 

//Bringing it all together 
var finalQuery = new Parse.Query("User"); 

//This is the part below I am talking about 
finalQuery.doesNotMatchKeyInQuery("objectId", "id1", class1Query); 
finalQuery.doesNotMatchKeyInQuery("objectId", "id2", class2Query); 
finalQuery.doesNotMatchKeyInQuery("objectId", "id3", class3Query); 

finalQuery.find({ 
    success: function (results) { 
     response.success(results); 
    }, 
    error: function (error) { 
     response.error(error); 
    } 
}); 

回答

-1

無法在單個請求中執行如此複雜的查詢。但是,您可以提前獲取不想匹配的密鑰,然後從中構建輔助查詢。 我已經寫了基於上面的代碼示例:

// Assuming we're actually dealing with 3 different classes, 
// and these can't be combined into a single query 
var class1Query = new Parse.Query('Class1'); 
class1Query.equalTo('id', id1); 
var class2Query = new Parse.Query('Class2'); 
class2Query.equalTo('id', id2); 
var class3Query = new Parse.Query('Class3'); 
class3Query.equalTo('id', id3); 
// Fetch the results from all three queries simultaneously 
Parse.Promise.when([ 
    class1Query.find(), 
    class2Query.find(), 
    class3Query.find() 
]).then(function(results) { 
    // results will contain three arrays of results 
    // We can now build a query where the objectId is not equal 
    // to any of the objectIds of the results 
    var ids = []; 
    results.forEach(function(set) { 
    set.forEach(function(obj) { 
     ids.push(obj.id); 
    }); 
    }); 
    return new Parse.Query('FinalClass').notContainedIn('objectId', ids).find(); 
}) 

我想提醒你,這個查詢將不會高效地爲大型數據集。 「不等於」查詢永遠不會很快,因爲它們必須遍歷表中的每個對象。如果有另一種方式來獲取您的數據,我高度鼓勵它。

+0

我其實認爲這是行不通的。只有其中一個查詢是填充結果而不是所有的查詢。 – Midevilworm 2015-04-06 17:44:51

+0

其實,我明白了。對於任何想知道每個.find方法的人來說,都需要在函數返回中附帶一個結果變量var。它看起來像這樣 ])。然後(功能(results1,results2,results3){ – Midevilworm 2015-04-06 17:51:40

+0

@Midevilworm你能寫出完整的答案嗎? – Idan 2016-02-14 14:06:41