我的 /app/api/auth/route.ts 文件:
import { redirect } from 'next/navigation';
export async function GET(req: Request) {
try {
redirect('/dashboard');
} catch (error) {
console.log(error);
redirect('/');
}
}
Run Code Online (Sandbox Code Playgroud)
我意识到当我在 try catch 中进行重定向时,我收到错误:
Error: NEXT_REDIRECT
at getRedirectError (webpack-internal:///(sc_server)/./node_modules/next/dist/client/components/redirect.js:40:19)
at redirect (webpack-internal:///(sc_server)/./node_modules/next/dist/client/components/redirect.js:46:11)
at GET (webpack-internal:///(sc_server)/./app/api/auth/route.ts:23:66)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:244:37) {
digest: 'NEXT_REDIRECT;replace;/dashboard'
}
Run Code Online (Sandbox Code Playgroud)
当我摆脱 try catch 时,一切正常:
export async function GET(req: Request) {
redirect('/dashboard')
}
Run Code Online (Sandbox Code Playgroud)
这按预期工作。我需要尝试并捕获,因为这是一个身份验证路由,并且我需要一些错误处理,因为请求可能会失败,我省略了身份验证功能,因为我意识到这只是在简单的尝试和捕获上发生的。
或者,如果 Next 13 在 /api 路由中有另一种错误处理方式,请告诉我。
我正在使用 prettier,并使用 firebase init 和 eslint 选项初始化了我的项目。但是当我保存文件时,prettier 扩展会在对象花括号中添加间距,如下所示:
export { basicHTTP } from './http';
Run Code Online (Sandbox Code Playgroud)
这是我的 .eslintrc.js 文件,它与 firebase init 一起提供:
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript',
'google',
],
parser: '@typescript-eslint/parser',
parserOptions: {
project: ['tsconfig.json', 'tsconfig.dev.json'],
tsconfigRootDir: __dirname,
sourceType: 'module',
},
ignorePatterns: [
'/lib/**/*', // Ignore built files.
],
plugins: ['@typescript-eslint', 'import'],
rules: {
quotes: ['error'],
},
};
Run Code Online (Sandbox Code Playgroud) 当我运行 NextJs 程序时,总是会弹出这个错误
Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db
Run Code Online (Sandbox Code Playgroud)
然后我在 shell 中运行 npx browserslist@latest --update-db :
npx: installed 6 in 1.905s
Current version: 1.0.30001179
New version: 1.0.30001179
Removing old caniuse-lite from lock file
Installing new caniuse-lite version
$ npm install caniuse-lite
Cleaning package.json dependencies from caniuse-lite
$ npm uninstall caniuse-lite
caniuse-lite has been successfully updated
No target browser changes
Run Code Online (Sandbox Code Playgroud)
当我再次运行我的 Next 程序时,会发生同样的错误。这昨天开始发生在我身上。我只使用了 npx create-next-app 和 npm run dev。