1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Next.js】apiで返したstatusが受け取れない

Posted at

はじめに

 開発中、apiで返したstatusを受け取って処理をしたいと思っていましたがstatusを受け取ることができません。
 根本的なコード理解が不十分であったことが原因なのですが、こちらの解決方法について共有させていただきます。
 

【修正前のコード】

route.ts
return new Response("Not authenticated", { status: 401});
.tsx
const response = await res.json();

勘の良い方は分かると思いますが、apiでjsonで返していないものに対してjsonで受け取ろうとしていました。

解決方法

resで受け取る

.tsx
if (res.status === 401){
  // 実行内容
    }

参考

おわりに

どなたかの理解のきっかけになれば幸いです。


JISOUのメンバー募集中!

プログラミングコーチングJISOUでは、新たなメンバーを募集しています。
日本一のアウトプットコミュニティでキャリアアップしませんか?
興味のある方は、ぜひホームページをのぞいてみてください!
▼▼▼

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?