2014-04-09 81 views
33

我在Parse上有云功能。當它被調用時,它將檢索一個PFObject,然後在該對象和用戶之間添加一個關係。這部分工作正常(看到功能的結束)。 我遇到了麻煩,其選擇PFObject忽略那些用戶已經涉及到解析雲代碼關係查詢語法

這裏查詢是我的代碼:

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

    var LikeRequest = Parse.Object.extend("LikeRequest"); 
    var query = new Parse.Query(LikeRequest); 

    query.equalTo("completed", false); 
    console.log("user: " + Parse.User.current().id); 

    query.notEqualTo("user", Parse.User.current()); 

    // Also only fetch if never been sent before 
    // HERE SHOULD USE THE BELOW RELATIONSHIP 
    var innerQuery = new Parse.Query(Parse.User); 
    innerQuery.exists(Parse.User); 
    query.matchesQuery("sentTo", innerQuery); 

    query.ascending("createdAt"); 

    query.first({ 

     success: function (object) { 
      // Successfully retrieved the object. 
      console.log("Got 1 object: " + object.get('mediaId')); 

      // Record that the user has been sent it 
      var user = Parse.User.current(); 
      var relation = object.relation("sentTo"); // RELATION ADDED HERE 

      relation.add(user); 
      object.save(); 

      response.success(object); 
     }, 
     error: function (error) { 
      console.log("Error: " + error.code + " " + error.message); 

      response.error("Error getting next mediaId"); 
     } 
    }); 
}); 

我敢肯定,我只是不理解如何關係查詢語法起作用。

+0

在你的LikeRequest類中,你有一個用戶列嗎?我不理解query.notEqualTo(「user」,Parse.User.current()); – bmurmistro

+0

在添加關係的底部,添加了許多用戶列。 – Darren

+0

抱歉,延遲響應。你嘗試過這樣的事情: var innerQuery = LikeRequest.relation(「sendTo」)。query(); innerQuery.exists(Parse.User); – bmurmistro

回答

1

Query Constraints

如果你想獲取不匹配的幾個值的對象,你可以使用notContainedIn

// Finds objects from anyone who is neither Jonathan, Dario, nor Shawn 
query.notContainedIn("playerName", 
        ["Jonathan Walsh", "Dario Wunsch", "Shawn Simon"]); 
2

這片:

// Also only fetch if never been sent before 
// HERE SHOULD USE THE BELOW RELATIONSHIP 
var innerQuery = new Parse.Query(Parse.User); 
innerQuery.exists(Parse.User); 
query.matchesQuery("sentTo", innerQuery); 

可改爲:

// Also only fetch if never been sent before 
query.notContainedIn("sentTo",[Parse.User.current()]) 

工程。 Parse Query