Error: 在 getStaticPaths 中未提供必需的参数 (username) 作为字符串

huangapple go评论61阅读模式
英文:

Error: A required parameter (username) was not provided as a string in getStaticPaths

问题

下面是您要翻译的内容:

"Nextjs, Directus and Algolia to build a drivers page where you can click on their profile and see their info. To see someones info the link would be http://localhost:3000/drivers/username - Nothing too fancy. The issue I have is when I click a link I get Error: A required parameter (username) was not provided as a string in getStaticPaths The username is a string, so i don't understand it. Any direction would be appreciated. Thanks."

let config = {
  headers: {
    "authorization": 'Bearer xxx'
  }
}

export async function getStaticPaths() {

  const username = await fetch(`https://xxx.directus.app/users`,config).then(res => res.json());

  const paths = [username.data].map((driver) => ({
    params: {
      username: driver.username
    },
  }))

  return { paths, fallback: false }
}

export async function getStaticProps({ params }) {

  const results = await fetch(`https://xxx.directus.app/users/?fields=username&[username][_eq]=${params.username}`, config).then(res => res.json());
  return {
    props: {
      results
    },
  }
}

export default function Driver({ results }) {

  return (
    <div>
      <h1>{results.data[0].username}</h1>
    </div>
  )
}
英文:

Nextjs, Directus and Algolia to build a drivers page where you can click on their profile and see their info. To see someones info the link would be http://localhost:3000/drivers/username - Nothing too fancy.
The issue I have is when I click a link I get Error: A required parameter (username) was not provided as a string in getStaticPaths
The username is a string, so i don't understand it. Any direction would be appreciated. Thanks.

let config = {
  headers: {
    &quot;authorization&quot;: &#39;Bearer xxx&#39;
  }
}

export async function getStaticPaths() {

  const username = await fetch(`https://xxx.directus.app/users`,config).then(res =&gt; res.json());

  const paths = [username.data].map((driver) =&gt; ({
    params: {
      username: driver.username
    },
  }))

  return { paths, fallback: false }
}

export async function getStaticProps({ params }) {

  const results = await fetch(`https://xxx.directus.app/users/?fields=username&amp;[username][_eq]=${params.username}`, config).then(res =&gt; res.json());
  return {
    props: {
      results
    },
  }
}

export default function Driver({ results }) {

  return (
    `&lt;div&gt;
      &lt;h1&gt;{results.data[0].username}&lt;/h1&gt;
    &lt;/div&gt;`
  )
}

答案1

得分: 1

Most likely here username.data is already an array so you don't need to convert it to an array. Otherwise, you'll get this as paths: [ { params: { username: undefined } } ] since [username.data] contains only one element, which is username.data, and this latter does not have a property username because it is an array. So the returned username will be undefined.

英文:

Most likely here username.data is already an array so you don't need to convert it to array otherwise you'll get this as paths : [ { params: { username: undefined } } ] since [username.data] contains only one element which is username.data and this latter does not have a property username because it is an array so the returned username will be undefined.

const paths = username.data.map((driver) =&gt; ({
  params: {
    username: driver.username
  },
}))

huangapple
  • 本文由 发表于 2023年2月7日 04:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定