Skip to content

AfterSave BeforDelete does not update mongodb record on Server but works on Local Machine #2658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
willysharp5 opened this issue Sep 6, 2016 · 4 comments

Comments

@willysharp5
Copy link

willysharp5 commented Sep 6, 2016

AfterSave and BeforeDelete triggers updates records on my mongodb server when I run it locally but when I run the same code in the cloud it fails to update the record.In both cases Mongodb is running on a server. I am also triggering the record change via IOS parse SDK. I also do find that in the IOS terminal that it tries to reconnect to the server a bunch of times before working, however when I run the parse server locally I do not get the reconnect issues.

Steps to reproduce

Create a Cloud Code for beforeSave and aftersave
Run a simple IOS update or delete of a Class

Please include a detailed list of steps that reproduce the issue. Include curl commands when applicable.

  1. ATERSAVE CODE
Parse.Cloud.afterSave("Wallet", function(request, response) {
    var currentUser = request.user; //pointer to user object
    var SyncTable = Parse.Object.extend("SyncTable");

    var query = new Parse.Query(SyncTable);
    query.equalTo("syncTableUser", currentUser);
    query.equalTo("tableName","User");
    query.first({
        useMasterKey: true,
        success: function(object) {
                // Successfully retrieved the object.
                object.increment("version");
                object.set("isDelete", false);
                object.save();
            },
            error: function(error) {
                //error
                console.log("Message Error: "+error);
            }
        });
});

BEFOREDELETE

Parse.Cloud.beforeDelete("Wallet", function(request, response) {
  var currentUser = request.user; //pointer to user object
  var SyncTable = Parse.Object.extend("SyncTable");

  var query = new Parse.Query(SyncTable);
  query.equalTo("tableName","Wallet");
  query.equalTo("syncTableUser", currentUser);
  query.first({
        success: function(object) {
                // Successfully retrieved the object.
                object.increment("version");
                object.set("isDelete", true);
                object.save();
            },
            error: function(error) {
                //error
            }
        });


  response.success();
});

SWIFT IOS CODE

let wallet = Wallet_P()
wallet.uuid = wallet_r.uuid
wallet.walletUser = PFUser.currentUser()
wallet.currency = wallet_r.currency
wallet.type = wallet_r.type
wallet.categoryType = wallet_r.categoryType

//Relation
let bank = Bank_P()
bank.uuid = wallet_r.walletBank?.uuid
bank.BankName = wallet_r.walletBank?.BankName
bank.balance = wallet_r.walletBank?.balance
bank.participants = wallet_r.walletBank?.participants
bank.totalAmount = wallet_r.walletBank?.totalAmount
wallet.walletBank = bank

wallet.saveEventually()

Expected Results

MongoDB For bothe Local and Server Calls
SyncTable Should be AfterSave
version : 1 (incremented by 1)
isDelete : True

SyncTable Should be BeforeDelete
version : 1 (incremented by 1)
isDelete : False

Actual Outcome

MongoDB for Server
SyncTable Should be AfterSave
version : 0
isDelete : False

SyncTable Should be BeforeDelete
version : 0
isDelete : False

MongoDB for Local
SyncTable Should be AfterSave
version : 1
isDelete : True

SyncTable Should be BeforeDelete
version : 1
isDelete : False

Environment Setup

  • Server
    • parse-server version: 2.2.17 running on Node version v4.2.6
    • Operating System: Ubuntu 14.04.4
    • Hardware: 512MB Memmory 20GB Hard Drive
    • Remote Server: Digital Ocean
  • Local
    • parse-server version: 2.2.17 running on Node version v4.2.6
    • Operating System: OSX EL Capitan version 10.11.6
    • Hardware: 2.8Ghz Core i7
    • Localhost: Local Host
  • Database
    • MongoDB version: 3.0.11
    • Storage engine: MMAPv1
    • Hardware: Ubuntu 14.04.4
    • Remote Server: Digital Ocean

Logs/Trace

2016-09-04T19:35:50.522Z - afterSave triggered for Wallet

Input: {"ACL":{"*":{"read":true},"2lKftPhsKz":{"write":true,"read":true}},"uuid":"ZAi5rZHa1W","walletUser":{"__type":"Pointer","className":"_User","objectId":"2lKftPhsKz"},"type":"Bank","currency":"USD -$","categoryType":"Bank","walletBank":{"__type":"Pointer","className":"Bank","objectId":"6TzvC1JCUV"},"createdAt":"2016-09-04T19:35:50.389Z","updatedAt":"2016-09-04T19:35:50.389Z","objectId":"TfKBK3WCiA"}
@flovilmart
Copy link
Contributor

flovilmart commented Sep 6, 2016

BEFORE SAVE CODE
 Parse.Cloud.afterSave("Wallet", function(request, response) {

Not sure if you meant AFTER SAVE or beforeSave.

In anycase, afterSave is async as we don't wait for the result of the afterSave to return unless you return a promise and only when that PR will land (#2499) in 2.2.19.

In your beforeDelete, you should call  response.success();  only after object.save() completes, the call to

object.increment("version");
object.set("isDelete", true);

are completely async in your case and the deletion continue without waiting for it to complete.

@willysharp5
Copy link
Author

willysharp5 commented Sep 6, 2016

Thanks @flovilmart for the quick response,
I meant afterSave and beforeDelete, sorry for the Typos. I also did run the code using promises for afterSave and still did not get the expected result of my MongoDb record updated. However since you did mention promises and when I run this code on my actual IOS device, the app gets stuck in a loop for a while and still does not update the record.

I also got similar issues with running this triggers on a Table that has an Image attachment.

So how would I update my code to run promises? I tried using this and still no change. However it runs locally fine and updates MongoDB.

Parse.Cloud.aferSave("Wallet", function(request, response) {
  var currentUser = request.user; 
  var SyncTable = Parse.Object.extend("SyncTable");
  var query = new Parse.Query(SyncTable);
  query.equalTo("tableName",tableName);
  query.equalTo("syncTableUser", currentUser);
  Parse.Promise.when(query.first).then(function(res1){
        res1.increment("version");
        res1.set("isDelete", false);
        res1.save();

    }, function(e){
    response.error();
  })
});

Is there a fix before version 2.2.19 or just wait.
Is this normal for the code to run fine locally and not run on server or is my server not set up correctly?
Will I be able to handle this promises in IOS SDK or is this just a fix for REST

@flovilmart
Copy link
Contributor

If it works locally that's probably not an issue on parse-server but in your code. We don't provide code level support and this area is extensively tested. I suggest you reach out to a separate channel like stackoverflow for those implementation details.

@willysharp5
Copy link
Author

willysharp5 commented Sep 6, 2016

Ok could you share sample code on how a promise should be done for both afterSave and beforeDelete. I do think its the server but most likely not with my server but with the triggers and promises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants