0

I've got this error in my Parse cloud code and I don't really understand how THIS SOLUTION applies to my code because I am using Parse cloud code. I do understand that the server is a via which runs Parse using javascript, which then runs CRUD actions in the database. But because it can't really be seen by the actual HTTP requests I can't tell whats happening and where I should adjust my code.

If someone could point out to me what I'm doing wrong and how I could adjust my code that would be great. Also is this even a big deal? If I leave my cloud code like below in production would this be a big deal? Everything runs perfectly as per high level results right now with this function.

I have the idea I should be continuing the .then() funtions. If this is the case, just how many long can this tree go?

Parse.Cloud.define("ccCreateGroupRole", async function(request){
    let user = request.user.id;
    let acl = new Parse.ACL();
    acl.setReadAccess(request.user, true);
    acl.setWriteAccess(request.user, true);
    let adminRole = new Parse.Role("Group_" + user, acl);
    adminRole.save(null, { useMasterKey: true }).then(function() {
        adminRole.getUsers().add(request.user);
        adminRole.save(null, { useMasterKey: true }).then(function() {
            console.log("Role created " + adminRole.id);
        });
    });

    // Create Sync Status objects
    let masterSyncStatus = {user: user, syncStatus: true};
    let newUserSyncStatus = {user: user, syncStatus: true};
    let masterArray = [];
    let newUserArray = [];
    masterArray.push(masterSyncStatus);
    newUserArray.push(newUserSyncStatus);

    // Initialize Sync Status class object by groupID
    let SyncStatus = Parse.Object.extend("SyncStatus");
      let syncStatus = new SyncStatus();
      syncStatus.set("groupName", "Group_" + user);
      syncStatus.set("masterSyncStatus", masterArray);
      syncStatus.set("newUserSyncStatus", newUserArray);
    await syncStatus.save(null, { useMasterKey: true });

    // Write own publicKey to user class
    let UserAddPublicKey = Parse.Object.extend("_User");
    let userAddPublicKey = new UserAddPublicKey();
    userAddPublicKey.set("objectId", request.user.id);
    userAddPublicKey.save(null, { useMasterKey: true });
  });

RobbB
  • 1,214
  • 11
  • 39
  • 1
    The error you refer to has to do with trying to send more than one response to the same http request, but you don't show ANY sending of any http responses in this code, so I think we need to see more code - ideally the entire request handler that this is part of. Furthermore, this is typically an error from an http request handler or an express route handler. We need to see that handler in its entirety. – jfriend00 Nov 21 '21 at 06:09
  • That makes sense, this is definitely part of my problem that I'm not sure how to acquire that while using Parse. I will look into it and post it when I have gotten the handler. – RobbB Nov 21 '21 at 06:14
  • So, I don't know the Parse system at all. Are there method calls in this code that are supposed to lead directly to an http response being sent? – jfriend00 Nov 21 '21 at 06:36
  • Yes, each call leads directly to an HTTP request. I thought that awaiting the function would work, well it does but throws the errors also... I have solved this please see my solution below. Posting now. I think this is very weird having many .then({}) in a chain for many actions in one function... – RobbB Nov 21 '21 at 07:15

2 Answers2

1

Your original code was not awaiting all the promises you have. Instead, you were using a mix of await and .then(), but not controlling the flow properly everywhere.

When you nested all the .then() inside of each other, you were able to properly sequence all the asynchronous operations so that B waited until A was done and C waited until B was done. Your original code was not doing that.

But, you can do all that with await too like this by using await on all asynchronous operations to make sure you're sequencing everything:

Parse.Cloud.define("ccCreateGroupRole", async function(request) {
    let user = request.user.id;
    let acl = new Parse.ACL();
    acl.setReadAccess(request.user, true);
    acl.setWriteAccess(request.user, true);
    let adminRole = new Parse.Role("Group_" + user, acl);
    await adminRole.save(null, { useMasterKey: true });
    adminRole.getUsers().add(request.user);
    await adminRole.save(null, { useMasterKey: true });
    console.log("Role created " + adminRole.id);

    // Create Sync Status objects
    let masterSyncStatus = { user: user, syncStatus: true };
    let newUserSyncStatus = { user: user, syncStatus: true };
    let masterArray = [];
    let newUserArray = [];
    masterArray.push(masterSyncStatus);
    newUserArray.push(newUserSyncStatus);

    // Initialize Sync Status class object by groupID
    let SyncStatus = Parse.Object.extend("SyncStatus");
    let syncStatus = new SyncStatus();
    syncStatus.set("groupName", "Group_" + user);
    syncStatus.set("masterSyncStatus", masterArray);
    syncStatus.set("newUserSyncStatus", newUserArray);
    await syncStatus.save(null, { useMasterKey: true });

    // Write own publicKey to user class
    let UserAddPublicKey = Parse.Object.extend("_User");
    let userAddPublicKey = new UserAddPublicKey();
    userAddPublicKey.set("objectId", request.user.id);
    await userAddPublicKey.save(null, { useMasterKey: true });
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you for pointing that out. Problems solved and understanding increased. – RobbB Nov 21 '21 at 07:41
  • @RobbB - What about error handling here? What if any of those promises you are awaiting reject? Will Parse catch the rejection and do something intelligible? Or do you have to catch the rejection and do something appropriate? – jfriend00 Nov 21 '21 at 07:43
  • I am currently going through all my cloud code now and working on handling possible errors. Initially I was concerned with learning javascript and just "getting things to work" now it's refactor time. – RobbB Nov 21 '21 at 07:52
0

I'm not sure if this is "javascript correct" but I am chaining together many .then({}) and I do not get an error anymore. I don't understand why the async/await I was using before does not work... but this solves my main question.

EDIT: The async/await did work but I get the error in Parse logs.

Can anyone explain this better? If so correct answer is still up for grabs.

Parse.Cloud.define("ccCreateGroupRole", async function(request) {
    let user = request.user.id;
    let acl = new Parse.ACL();
    acl.setReadAccess(request.user, true);
    acl.setWriteAccess(request.user, true);
    let adminRole = new Parse.Role("Group_" + user, acl);
    adminRole.save(null, { useMasterKey: true }).then(function() {
        adminRole.getUsers().add(request.user);
        adminRole.save(null, { useMasterKey: true }).then(function() {
            console.log("Role created " + adminRole.id);
            // Create Sync Status objects
            let masterSyncStatus = { user: user, syncStatus: true };
            let newUserSyncStatus = { user: user, syncStatus: true };
            let masterArray = [];
            let newUserArray = [];
            masterArray.push(masterSyncStatus);
            newUserArray.push(newUserSyncStatus);
            // Initialize Sync Status class object by groupID
            let SyncStatus = Parse.Object.extend("SyncStatus");
            let syncStatus = new SyncStatus();
            syncStatus.set("groupName", "Group_" + user);
            syncStatus.set("masterSyncStatus", masterArray);
            syncStatus.set("newUserSyncStatus", newUserArray);
            syncStatus.save(null, { useMasterKey: true }).then(function() {
                // Write own publicKey to user class
                let UserAddPublicKey = Parse.Object.extend("_User");
                let userAddPublicKey = new UserAddPublicKey();
                userAddPublicKey.set("objectId", request.user.id);
                userAddPublicKey.save(null, { useMasterKey: true }).then(function() {
                    console.log("Save success");
                });
            });
        });
    });
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
RobbB
  • 1,214
  • 11
  • 39