Unverified Commit 1c078482 authored by Farsab's avatar Farsab Committed by GitHub

fix(docs): adjust api route documentation examples (#56660)

Fixed the example functions in the documentation to use `async` in order to use `await` within the function body. 
Co-authored-by: 's avatarMichael Novotny <446260+manovotny@users.noreply.github.com>
parent f6d6acd9
...@@ -217,7 +217,10 @@ The following example sends a JSON response with the status code `200` (`OK`) an ...@@ -217,7 +217,10 @@ The following example sends a JSON response with the status code `200` (`OK`) an
```ts filename="pages/api/hello.ts" switcher ```ts filename="pages/api/hello.ts" switcher
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try { try {
const result = await someAsyncOperation() const result = await someAsyncOperation()
res.status(200).json({ result }) res.status(200).json({ result })
...@@ -228,7 +231,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) { ...@@ -228,7 +231,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
``` ```
```js filename="pages/api/hello.js" switcher ```js filename="pages/api/hello.js" switcher
export default function handler(req, res) { export default async function handler(req, res) {
try { try {
const result = await someAsyncOperation() const result = await someAsyncOperation()
res.status(200).json({ result }) res.status(200).json({ result })
...@@ -247,7 +250,10 @@ The following example sends a HTTP response with the status code `200` (`OK`) an ...@@ -247,7 +250,10 @@ The following example sends a HTTP response with the status code `200` (`OK`) an
```ts filename="pages/api/hello.ts" switcher ```ts filename="pages/api/hello.ts" switcher
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try { try {
const result = await someAsyncOperation() const result = await someAsyncOperation()
res.status(200).send({ result }) res.status(200).send({ result })
...@@ -258,7 +264,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) { ...@@ -258,7 +264,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
``` ```
```js filename="pages/api/hello.js" switcher ```js filename="pages/api/hello.js" switcher
export default function handler(req, res) { export default async function handler(req, res) {
try { try {
const result = await someAsyncOperation() const result = await someAsyncOperation()
res.status(200).send({ result }) res.status(200).send({ result })
...@@ -277,7 +283,10 @@ The following example redirects the client to the `/` path if the form is succes ...@@ -277,7 +283,10 @@ The following example redirects the client to the `/` path if the form is succes
```ts filename="pages/api/hello.ts" switcher ```ts filename="pages/api/hello.ts" switcher
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { name, message } = req.body const { name, message } = req.body
try { try {
...@@ -290,7 +299,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) { ...@@ -290,7 +299,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
``` ```
```js filename="pages/api/hello.js" switcher ```js filename="pages/api/hello.js" switcher
export default function handler(req, res) { export default async function handler(req, res) {
const { name, message } = req.body const { name, message } = req.body
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment