发送给客户端后无法设置 headers - 请帮助我在我的代码中理解这一点

Cannot set headers after they are sent to the client - Please help me understand this in my code

我在我的 Parse 云代码中遇到了这个错误,我真的不明白 THIS SOLUTION 如何应用于我的代码,因为我正在使用 Parse 云代码。我确实知道服务器是一个使用 javascript 运行 Parse 的通道,然后在数据库中运行 CRUD 操作。但是因为实际的 HTTP 请求无法真正看到它,所以我不知道发生了什么以及我应该在哪里调整我的代码。

如果有人能指出我做错了什么以及我如何调整我的代码,那就太好了。这也有什么大不了的?如果我在生产中像下面这样保留我的云代码,这会有什么大不了的吗?使用此功能,一切都按照高级结果完美运行。

我觉得我应该继续 .then() 函数。如果是这样的话,这棵树到底能长多少?

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 });
  });

我不确定这是否“javascript 正确”,但我将许多 .then({}) 链接在一起并且我不再收到错误。我不明白为什么我之前使用的 async/await 不起作用...但这解决了我的主要问题。

编辑:async/await 确实有效,但我在 Parse 日志中收到错误。

谁能更好地解释一下?如果是这样,正确的答案还有待争取。

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");
                });
            });
        });
    });
});

您的原始代码并未等待您的所有承诺。相反,您混合使用了 await.then(),但没有正确控制所有地方的流量。

当您将所有 .then() 相互嵌套时,您可以正确地对所有异步操作进行排序,以便 B 等到 A 完成,C 等到 B 完成。您的原始代码没有这样做。

但是,您也可以使用 await 完成所有这些操作,方法是在所有异步操作上使用 await 以确保您对所有内容进行排序:

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 });
});