【问题标题】:Express Js Firebase SignOut() immeditately returning status 200 to client side Fetch Post requestExpress Js Firebase SignOut() 立即向客户端 Fetch Post 请求返回状态 200
【发布时间】:2019-09-27 23:25:27
【问题描述】:

我正在使用 Fetch 在我的客户端 javascript 上向使用 Express Js 和 Firebase Auth 的服务器端 javascript 发出 POST 请求。 auth().signOut() 返回 200 代码,但我的代码的 .then 部分永远不会执行,因为该函数会立即将响应返回给客户端获取;因此永远不会呈现我的下一个视图。仅在执行 .then 部分服务器端代码后,如何让服务器端代码向客户端代码返回 200 状态?

这是我的代码:

服务器端:

var logoutController = {

    //post logout user function
    logout: function(req, res, next) {
        var context, view;

        firebase.auth().signOut().then(function(data){
            view = 'index';
            context = {
                error: false,
            };
            res.render(view, context);

        }).catch(function(error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            view = 'index'; //Change to an error page
            context = {
                error: true,
            };
            res.render(view, context);
        });

    }, 

} //end logoutController

router.post("/logout", logoutController.logout);

客户端代码:

var button = document.getElementById("logoutButton");

button.addEventListener('click', function(e) {

    fetch('/logout', {
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    })
    .then(function(response) {
      if(response.ok) {
        return;
      }
      throw new Error('Request failed.');
    })
    .catch(function(error) {
      alert(error);
    });

});

【问题讨论】:

    标签: javascript node.js firebase express firebase-authentication


    【解决方案1】:

    我找到的解决方案是在客户端使用:

    window.location.pathname = '/'

    重定向到我希望用户在他注销后转到的页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2020-09-02
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 2021-03-12
      相关资源
      最近更新 更多