简体   繁体   English

从 getStaticProps 内部调用时如何将查询参数传递给 next.js api 处理程序

[英]How to pass query parameters to next.js api handler when calling from inside of getStaticProps

I am under the impression that you can import api request handlers and call them directly inside of your getStaticProps function because of the documentation where it says:我的印象是您可以导入 api 请求处理程序并直接在您的 getStaticProps function 内部调用它们,因为文档中说:

Note: You should not use fetch() to call an API route in your application.注意:您不应在应用程序中使用 fetch() 来调用 API 路由。 Instead, directly import the API route and call its function yourself.相反,直接导入 API 路由并自己调用其 function。 You may need to slightly refactor your code for this approach.您可能需要为这种方法稍微重构您的代码。

I have an api route at /api/user/[id].js and the implementation looks something like this:我在/api/user/[id].js有一个 api 路由,实现看起来像这样:

export default function user(req, res) {
  const userId = req.query.id;
  const user = getUserById(userId);
  res.json(user);
}

If I want to use this handler in the getStaticProps of another page in the front-end such as /admin/user/[id].js how would I pass the id query to the request handler?如果我想在前端的另一个页面的getStaticProps中使用此处理程序,例如/admin/user/[id].js我将如何将id查询传递给请求处理程序? Calling it without any parameters doesn't work and throws an error saying that req.query.id is undefined.在没有任何参数的情况下调用它是行不通的,并会抛出一个错误,指出req.query.id未定义。

import userHandler from "../../api/user/[id].js";

export async getStaticProps(){
    // This is where the api handler is getting called
    const user = await userHandler();
    return { props: { user } }
}

Here is what I would suggest to do in order to make stuff work:这是我建议做的事情,以使事情正常进行:

  1. you do not need "dynamic" name for the file with your api calls handlers (instead of /api/user/[id].js you can create /api/user.js );您的 api 调用处理程序不需要文件的“动态”名称(您可以创建/api/user.js /api/user/[id].js
  2. you need specify a page (file) for user details view.您需要为用户详细信息视图指定一个页面(文件)。 It should be created in /pages/user/[id].js and paste getStaticProps function there.它应该在/pages/user/[id].js中创建并在其中粘贴getStaticProps function。 Now once you change url in browser to http://localhost:3000/user/whatever getStaticProps will be called with ({ params: {id: 'whatever'}})现在,一旦您将浏览器中的 url 更改为http://localhost:3000/user/whatever getStaticProps将被调用 ({ params: {id: 'whatever'}})

getStaticProps - gets context argument which consists of several properties. getStaticProps - 获取包含多个属性的context参数。 All the dynamic URL parts will be stored under params property, taking into account above part this should work:所有动态 URL 部分都将存储在params属性下,考虑到以上部分这应该有效:

export async getStaticProps({ params }){
    const user = await user(params.id);
    return { props: { user } }
}

If you need some additional explanation you are welcome to ask如果您需要一些额外的解释,欢迎询问

You shouldn't call the API endpoint internally on the server.您不应在服务器内部调用 API 端点。 The user() handler in your API requires the Request and Response objects to be passed as arguments and there's no point in faking a HTTP request/response. API 中的user()处理程序要求RequestResponse对象作为 arguments 传递,伪造 HTTP 请求/响应没有意义。

Instead, export the function your API uses to get the user data: getUserById(userId) .相反, export function 您的 API 用于获取用户数据: getUserById(userId) Then, import and call it in getStaticProps() .然后, import并在getStaticProps()中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM