2016-05-12 32 views
0

我試圖通過解析雲代碼發送推送通知當某一物體已被修改 - 「髒」解析發送通知時,對象修改

我覺得我幾乎沒有,而是因爲我相信收到一個錯誤我正在創建一個新用戶而不是查詢一個。

Parse.Cloud.beforeSave("Fact", function(request, response) { 
    var dirtyKeys = request.object.dirtyKeys(); 
    for (var i = 0; i < dirtyKeys.length; ++i) { 
    var dirtyKey = dirtyKeys[i]; 
    if (dirtyKey === "isValid") { 

     //send push 
     // Creates a pointer to _User with object id of userId 

     var targetUser = new Parse.User(); 
     // targetUser.id = userId; 
     targetUser.id = request.object.userID; 

     var query = new Parse.Query(Parse.Installation); 
     query.equalTo('user', targetUser); 

     Parse.Push.send({ 
     where: query, 
     data: { 
      alert: "Your Fact was approved :)" 
     } 
     }); 

     return; 
    } 
    } 
    response.success(); 
}); 

我發現this post與我的問題有關。我現在的問題是如何將用戶查詢集成到我的beforeSave塊中。理想情況下,我會爲用戶查詢創建另一個函數,並將其放在我的beforeSave塊中。

** 5/14更新 我採取了@ toddg的建議,並修復了之前的保存。這是我想要做的和新的錯誤的更清晰的圖片。

Updating of the entry error

+0

將通知碼放入保存塊後更好。在上面的代碼中,response.success()可能在發送推送通知之前運行。也是回報;阻止它通知response.success()或response.error()。我建議將通知內容移至afterSave – Subash

+0

您通過這種方式得到的錯誤是什麼? – toddg

+0

我得到這個錯誤 - 「無法創建一個指向未保存的ParseObject的指針」 – mparrish91

回答

0

幾個點(如@Subash在評論中指出的)之前,我到代碼:

  1. Parse.Push.send是一個異步操作,所以你要確保你推送完成後請致電response.success()。我將使用Promises來處理這個問題,因爲我認爲它們比回調更靈活。如果你不熟悉,請閱讀它們here
  2. if語句的返回可能會阻止response.success()被調用。

這裏是我的建議做它的方式:

Parse.Cloud.beforeSave("Fact", function(request, response) { 

    // Keep track of whether we need to send the push notification 
    var shouldPushBeSent = false; 

    var dirtyKeys = request.object.dirtyKeys(); 
    for (var i = 0; i < dirtyKeys.length; ++i) { 
    var dirtyKey = dirtyKeys[i]; 

    if (dirtyKey === "isValid") { 
     shouldPushBeSent = true; 
    } 
    } 

    if (shouldPushBeSent) { 

     //send push 
     // Creates a pointer to _User with object id of userId 

     var targetUser = new Parse.User(); 
     // targetUser.id = userId; 
     targetUser.id = request.object.userId; 

     var query = new Parse.Query(Parse.Installation); 

     // We want to pass the User object to the query rather than the UserId 
     query.equalTo('user', targetUser); 
     Parse.Push.send({ 
     where: query, // Set our Installation query 
     data: { 
      alert: "Your fact was approved" 
     } 
     }).then(function(){ 

     // Now we know the push notification was successfully sent 
     response.success(); 
     }, function(error){ 

     // There was an error sending the push notification 
     response.error("We had an error sending push: " + error); 
     }); 
    } else { 
    // We don't need to send the push notification. 
    response.success(); 
    } 
}); 

順便說一句,我假設您在安裝類,用來跟蹤其用戶與每個安裝相關的有一列。

+0

感謝您的及時響應!這看起來不錯,很快就會實現。我有一個安裝類,但我沒有一個用戶ID列。那是我需要補充的嗎?然後我將不得不從iOS應用程序發送該用戶ID到Parse? – mparrish91

+0

對於正在嘗試添加列進行安裝的其他人來說,非常簡單。這是我如何做 '讓用戶= PFUser.currentUser() 讓currentInstallation = PFInstallation.currentInstallation() currentInstallation.setObject((用戶.objectId)!forKey:?! 「用戶ID」) currentInstallation.saveInBackground() ' – mparrish91