2017-04-01 74 views
0

我在解析服務器v2.3.7(其中包括Parse JS SDK v1.9.2)中排序複合查詢時遇到問題。我可以在原始查詢中使用升序/降序查詢方法,但是當我克隆它時,它們不再起作用。任何想法,爲什麼這不工作,或有更好的方法克隆查詢?下劃線_.clone()不適用於解析JS SDK複合查詢

var firstQuery = new Parse.Query('Object'); 
firstQuery.equalTo('property', 1); 
var secondQuery = new Parse.Query('Object'); 
secondQuery.equalTo('property', 2); 

var query = Parse.Query.or(firstQuery, secondQuery); 
query.descending('updatedAt'); 
// This works 
var clonedQuery = _.clone(query); 
clonedQuery.ascending('updatedAt'); 
// Throws error "clonedQuery.ascending is not a function" 

繼續這個問題:How can I clone a Parse Query using the Javascript SDK on Parse Cloud Code?

+0

好的,甚至可以使用Underscore.js。我更新了我的答案。 – user7771338

回答

1

更新

有一件事Lodash.js做這Underscore.js沒有。 Lodash.js複製原型,而Underscore.js將prototype設置爲undefined。要使它與Underscore.js一起使用,您需要手動複製原型:

var Parse = require('parse').Parse; 
var _ = require('underscore'); 

var firstQuery = new Parse.Query('Object'); 
firstQuery.equalTo('property', 1); 
var secondQuery = new Parse.Query('Object'); 
secondQuery.equalTo('property', 2); 

var query = Parse.Query.or(firstQuery, secondQuery); 
query.descending('updatedAt'); 
// This works 
var clonedQuery = _.clone(query); 
Object.setPrototypeOf(clonedQuery, Object.getPrototypeOf(query)); // This line is very important! 
clonedQuery.ascending('updatedAt'); 
// This works too 
+0

嗯,我實際上使用下劃線v1.8.3而不是lodash。我也剛剛檢查過,因爲我使用的是parse-server,實際上我使用的是parse v1.9.2。更新問題... – enjoyjeremy

+0

我會在我現有的代碼之外的孤立測試中嘗試一下。 – enjoyjeremy

+0

@enjoyjeremy。好的,甚至可以使用Underscore.js。我更新了我的答案。 – user7771338