动态路由问题 - Next.js(与 typescript 一起)

时间:2021-06-14 22:28:08

标签: javascript typescript next.js

我是 next.js 的新手,我正在尝试使用生成这条路线 **pages/perfil/[name]**

主要问题是,在该页面中,我使用的来自 API 调用的数据基于 id,但我不希望该 id 出现在路由中,我希望名称出现在那里。

让我告诉你我是怎么做到的

这是界面的样子,以便您了解数据

export type userFriends = {
  _id?: string;
  name?: string;
  perfil?: string;
  identifier?: string;
  notification?: boolean;
  friend?: boolean;
  createdAt?: string;
};

export type user = {
  _id?: string;
  name?: string;
  email?: string;
  password?: string;
  friends?: userFriends[];
  banner?: string;
  perfil?: string;
};

export interface IuserData {
  data: user;
}

export type multipleUsers = { data: user[] };

逻辑在这里

export const getStaticPaths: GetStaticPaths = async () => {
  const { data }: multipleUsers = await axios.get(
    "http://localhost:5000/api/user"
  );

  const paths = data.map(path => {
    return { params: { name: path.name, id: path._id } };
  });

  return { paths, fallback: true };
};

export const getStaticProps: GetStaticProps = async ({ params }) => {
  const { data }: IuserData = await axios.get(
    `http://localhost:5000/api/user/singleU/${params.id}`
  );

  return {
    props: { data }
  };
};

这条路线只在我输入 [id] 而不是 [name] 时才有效,正如你所看到的,我在参数中有 name 和 id。

然后 next.js 一直抛出这个错误

Error: Request failed with status code 500
    at createError (C:\Users\diego cifuentes\Desktop\Portafolio\Fullstack Projects\Facebook - MERN ( with next.js )\fronted\node_modules\axios\lib\core\createError.js:16:15)
    at settle (C:\Users\diego cifuentes\Desktop\Portafolio\Fullstack Projects\Facebook - MERN ( with next.js )\fronted\node_modules\axios\lib\core\settle.js:17:12)
    at IncomingMessage.handleStreamEnd (C:\Users\diego cifuentes\Desktop\Portafolio\Fullstack Projects\Facebook - MERN ( with next.js )\fronted\node_modules\axios\lib\adapters\http.js:260:11)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  config: {
    url: 'http://localhost:5000/api/user/singleU/undefined',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.21.1'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    adapter: [Function: httpAdapter],
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    validateStatus: [Function: validateStatus],
    data: undefined
  },
  request: <ref *1> ClientRequest {
    _events: [Object: null prototype] {
      socket: [Function (anonymous)],
      abort: [Function (anonymous)],
      aborted: [Function (anonymous)],
      connect: [Function (anonymous)],
      error: [Function (anonymous)],
      timeout: [Function (anonymous)],
      prefinish: [Function: requestOnPrefinish]
    },
    _eventsCount: 7,
    _maxListeners: undefined,
    outputData: [],
    outputSize: 0,
    writable: true,
    destroyed: true,
    _last: true,
    chunkedEncoding: false,
    shouldKeepAlive: false,
    _defaultKeepAlive: true,
    useChunkedEncodingByDefault: false,
    sendDate: false,
    _removedConnection: false,
    _removedContLen: false,
    _removedTE: false,
    _contentLength: 0,
    _hasBody: true,
    _trailer: '',
    finished: true,
    _headerSent: true,
    socket: Socket {
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'localhost',
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: null,
      _server: null,
      parser: null,
      _httpMessage: [Circular *1],
      [Symbol(async_id_symbol)]: 662958,
      [Symbol(kHandle)]: null,
      [Symbol(kSetNoDelay)]: false,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: null,
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesRead)]: 294,
      [Symbol(kBytesWritten)]: 154,
      [Symbol(RequestTimeout)]: undefined
    },
    _header: 'GET /api/user/singleU/undefined HTTP/1.1\r\n' +
      'Accept: application/json, text/plain, */*\r\n' +
      'User-Agent: axios/0.21.1\r\n' +
      'Host: localhost:5000\r\n' +
      'Connection: close\r\n' +
      '\r\n',
    _keepAliveTimeout: 0,
    _onPendingData: [Function: noopPendingOutput],
    agent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      defaultPort: 80,
      protocol: 'http:',
      options: [Object],
      requests: {},
      sockets: {},
      freeSockets: {},
      keepAliveMsecs: 1000,
      keepAlive: false,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      scheduling: 'fifo',
      maxTotalSockets: Infinity,
      totalSocketCount: 0,
      [Symbol(kCapture)]: false
    },
    socketPath: undefined,
    method: 'GET',
    maxHeaderSize: undefined,
    insecureHTTPParser: undefined,
    path: '/api/user/singleU/undefined',
    _ended: true,
    res: IncomingMessage {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      socket: [Socket],
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: [Object],
      rawHeaders: [Array],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 500,
      statusMessage: 'Internal Server Error',
      client: [Socket],
      _consuming: false,
      _dumped: false,
      req: [Circular *1],
      responseUrl: 'http://localhost:5000/api/user/singleU/undefined',
      redirects: [],
      [Symbol(kCapture)]: false,
      [Symbol(RequestTimeout)]: undefined
    },
    aborted: false,
    timeoutCb: null,
    upgradeOrConnect: false,
    parser: null,
    maxHeadersCount: null,
    reusedSocket: false,
    host: 'localhost',
    protocol: 'http:',
    _redirectable: Writable {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _options: [Object],
      _ended: true,
      _ending: true,
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 0,
      _requestBodyBuffers: [],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [Circular *1],
      _currentUrl: 'http://localhost:5000/api/user/singleU/undefined',
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false,
    [Symbol(kNeedDrain)]: false,
    [Symbol(corked)]: 0,
    [Symbol(kOutHeaders)]: [Object: null prototype] {
      accept: [Array],
      'user-agent': [Array],
      host: [Array]
    }
  },
  response: {
    status: 500,
    statusText: 'Internal Server Error',
    headers: {
      'x-powered-by': 'Express',
      'access-control-allow-origin': '*',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '36',
      etag: 'W/"24-z7bS9qns7nLyRRBbZiCqtiTHQlM"',
      date: 'Mon, 14 Jun 2021 22:20:50 GMT',
      connection: 'close'
    },
    config: {
      url: 'http://localhost:5000/api/user/singleU/undefined',
      method: 'get',
      headers: [Object],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      adapter: [Function: httpAdapter],
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      validateStatus: [Function: validateStatus],
      data: undefined
    },
    request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [Socket],
      _header: 'GET /api/user/singleU/undefined HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'User-Agent: axios/0.21.1\r\n' +
        'Host: localhost:5000\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/api/user/singleU/undefined',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'localhost',
      protocol: 'http:',
      _redirectable: [Writable],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    data: { Error: 'API request went wrong !' }
  },
  isAxiosError: true,
  toJSON: [Function: toJSON]
}

我能做什么?

感谢您的时间!

1 个答案:

答案 0 :(得分:1)

getStaticPaths 用于在静态生成站点时填充给定动态路由的潜在参数。传递给 paramsgetStaticProps 值将是在路由中传递的参数。没有“隐藏的”路由参数。

您可以使用的一种方法是记住用户参数,以便在静态站点生成期间 getStaticProps 可以参考先前在运行 getStaticPaths 时获取的数据:

const memoize = fn => {
  let promise, result;
  return () => {
    if (result) {
      return result;
    } else if (promise) {
      return promise;
    } else {
      promise = fn();
      try {
        result = await promise;
        return result;
      } finally {
        promise = null; // avoid leak
      }
    }
  }
}

const getUserLookup = memoize(async () => {
  const { data }: allUsers = await axios.get(
    "http://localhost:5000/api/user"
  );
  userIdLookup = new Map(
    data.map(({ _id, name}) => [name, _id])
  );
  return userIdLookup;
});

export const getStaticPaths: GetStaticPaths = async () => {
  const userNameIds = await getUserLookup();

  return {
    paths: [...userNameIds.entries()].map(([name, id]) => ({ name, id })),
    fallback: true
  };
};

export const getStaticProps: GetStaticProps = async ({ params }) => {
  const lookup = await getUserLookup();
  const { data }: IuserData = await axios.get(
    `http://localhost:5000/api/user/singleU/${lookup.get(params.name)}`
  );

  return {
    props: { data }
  };
};

因为静态站点的构建在同一个进程中运行,并且应该只对所有用户运行一次,所以应该避免多次获取用户。