完成所有功能后重定向

时间:2014-12-06 23:22:18

标签: javascript express parse-platform

我正在使用Parse托管和云代码功能。

我有一个运行函数的按钮,然后在调用函数后重定向到同一页面以刷新它。按钮调用的云函数然后从那里调用许多不同的函数,包括httpRequest。从我所看到的,页面在调用第一个函数后刷新,而不是后续函数和稍后调用的httpRequests。加载页面上的数据仍然显示旧数据,必须手动刷新才能查看更新的数据。

以下是按钮触发的代码:

  // User Control Panel -- Logic
  app.post('/panel', function(req, res) {
  var currentUser = Parse.User.current();
  if (currentUser) {
    currentUser.fetch().then(function(fetchedUser){
      var username = fetchedUser.getUsername();
      if (fetchedUser.get("timeRemaining") < 10) {
        res.redirect("/panel");
      } else if (fetchedUser.get("isRunning") == false){
        Parse.Cloud.run("dockerManager", {username: username}) // Ignoring the rest of the code, this is where the cloud function is called.
        res.redirect("/panel");
      } else {
        res.redirect("/panel");
      }
    }, function(error){
    });
  } else {
    res.redirect('/panel');
  }
});

这是正在运行的云功能:

Parse.Cloud.define("dockerManager", function(request, response) {
var username = request.params.username;
var override = request.params.override;
var containerID = request.params.containerID;

//other irrelevant code here    

} else {
  Parse.Cloud.useMasterKey();
  var query = new Parse.Query(Parse.User);
  query.equalTo("username", username);
  query.first(function(user) {
    if (user.get("dockerID") == undefined) {
      Parse.Cloud.run("createDockerContainer", {username: username});
      response.success("[Docker Manager] Created Docker Container for username: " + username + " with Docker ID: " + user.get("dockerID"));
    } else if (user.get("isRunning") == true) {
      Parse.Cloud.run("stopDockerContainer", {username: username});
      response.success("[Docker Manager] Stopped Docker Container for username: " + username + " with Docker ID: " + user.get("dockerID"));
    } else if (user.get("isRunning") == false) {
      if (user.get("timeRemaining") >= 10){
        Parse.Cloud.run("startDockerContainer", {username: username});
        response.success("[Docker Manager] Started Docker Container for username: " + username + " with Docker ID: " + user.get("dockerID"));
      } else {
        response.error("noTime");
      }
    }
  });
}

});

此调用的每个函数都将httpReqest发送到另一台服务器,如下所示:

Parse.Cloud.define("stopDockerContainer", function(request, response) {
  var username = request.params.username;

    //irrelevant code

    containerID = user.get("dockerID");
    Parse.Cloud.httpRequest({
      method: "POST",
      url: "http://[redacted address]/containers/" + containerID + "/stop",
      headers: {
        "Content-Type": "application/json"
      },
      success: function(httpResponse) {
        console.log("[Docker Stopper] Stopped Docker container for user: " + username + " with ID: " + containerID);
        user.set("isRunning", false);
        user.save();
        response.success(true);
      },
      error: function(httpResponse) {
        console.log("[Docker Stopper][CRITICAL] Error stopping docker container for username:" + username);
        console.log("Request failed with response code " + httpResponse.status);
        response.error(false);
      }
    });
  });
});

有什么想法吗?

0 个答案:

没有答案