reactjs I am getting this error SyntaxError: Invalid Unicode escape sequence at eval (< anonymous>) error adding console log in the getStaticProps()

0vvn1miw  于 2023-01-08  发布在  React
关注(0)|答案(1)|浏览(275)
    • 这是我的页面users. js**
import React from 'react'

const users = ({users}) => {
  return (
    <>    {
      users.map(user => {
        return <div key={user.id}> {user.name} </div>
      })
    }
    </>
  )
}


export default users

export async function getStaticProps() {
    const res = await fetch('https://jsonplaceholder.typicode.com/users')
    const users = await res.json()

    console.log("users", users)  

    return {
      props: {
        users,
      },
      // Next.js will attempt to re-generate the page:
      // - When a request comes in
      // - At most once every 10 seconds
      revalidate: 10, // In seconds
    }
  }
    • 当我运行此程序时,我在终端中收到以下错误,而我如何在终端中查看用户值?**
`error - SyntaxError: Invalid Unicode escape sequence
    at eval (<anonymous>)
    at oo_oo (webpack-internal:///./pages/users.js:50:14)
    at users (webpack-internal:///./pages/users.js:14:5)
    at renderWithHooks (C:\Users\udays\Documents\NEXTLEARN\next-pre-render\node_modules\react-dom\cjs\react-dom-server.browser.development.js:5658:16)

`

我试着去评论

`   reactStrictMode: true,`

但没有成功。
您的帮助是非常感谢!!

sdnqo3pr

sdnqo3pr1#

尝试添加一些类似的错误处理。

const res = await fetch('https://jsonplaceholder.typicode.com/users')
if (res.ok) {
  const users = await res.json()
  console.log("users", users)  
  return {
    props: {
      users,
    },
    // Next.js will attempt to re-generate the page:
    // - When a request comes in
    // - At most once every 10 seconds
    revalidate: 10, // In seconds
  }
} else {
  console.log('There was an error getting the data', res.status)
}

相关问题