向API发送get请求时,在nextjs geinitialProps中发生读取ECONNRESET错误

时间:2020-08-24 16:43:04

标签: node.js reactjs axios next.js

当我在_app.js文件的getInitialPros中使用axios发送获取请求时,此错误在next.js应用程序中发生。

 if (typeof window === "undefined") {
    // user = await checkAuth(ctx);

    // const token = ctx.req.headers.cookie;

    console.log("TOKEN", ctx.req.headers);

    if (ctx.req && ctx.req.headers.cookie) {
      try {
        res = await axiosClient("get", { cookie: ctx.req.headers.cookie }).get(
          "/auth/currentuser"
        );

        user = res.data;
        console.log("USER IN SERVER SIDE", user);
        ctx.store.dispatch(setAuthenticatedUser(res.data));
      } catch (err) {
        console.log("ERROR in APP", err);
        // console.log("USER FOUND IN APP.JS", res.data);
        ctx.store.dispatch(removeAuthenticatedUser());
      }
    }
  } else {
    try {
      res = await axiosClient("get").get("/auth/currentuser");

      user = res.data;
      // await checkAuth(ctx);

      // await checkAuth(ctx,)
      console.log("IN CLIENT", res.data);
    } catch (err) {}
  }

刷新页面时发生此错误,但仅在服务器端而不在客户端发生。

ERROR in APP Error: read ECONNRESET
        at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27) {
      errno: 'ECONNRESET',
      code: 'ECONNRESET',
      syscall: 'read',
      config: {
        url: '/auth/currentuser',
        method: 'get',
        headers: {
          Accept: 'application/json, text/plain, */*',
          cookie: 'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI1ZjNhYTJlMmQxN2YxMzAxYTA0NGUxYTIiLCJpYXQiOjE1OTgyODUyMDMsImV4cCI6MTU5ODI4ODgwM30.qtaW-D9P6tJHzL1uHZs3wlzF39UPVkPTLEieuqaVEJY',
          'User-Agent': 'axios/0.19.2'
        },
        baseURL: 'https://tatkaladda.com/api/',
        transformRequest: [ [Function: transformRequest] ],
        transformResponse: [ [Function: transformResponse] ],
        timeout: 0,
        adapter: [Function: httpAdapter],
        xsrfCookieName: 'XSRF-TOKEN',
        xsrfHeaderName: 'X-XSRF-TOKEN',
        maxContentLength: -1,
        validateStatus: [Function: validateStatus],
        data: undefined
      },

此错误仅发生在生产应用中,而未处于开发模式。

0 个答案:

没有答案
相关问题