javascript nextjs新的API路由有什么问题?

nbnkbykc  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(97)

我目前得到这个错误:SyntaxError:Unexpected token s in JSON at position 0我的代码是:

import { PrismaClient } from '@prisma/client';
import { IDPay } from 'idpay';
import { NextResponse } from 'next/server';

const prisma = new PrismaClient();
const apiKey = '';
const idpayAPI = new IDPay(apiKey);

export async function POST(req: Request) {
    console.log(req);
  const { id,order_id } = await req.json()
   
  const response = await idpayAPI.verifyPayment({
    id: id,
    order_id: order_id
  });

  return new NextResponse(JSON.stringify(response.status));
}

字符串
我想从发布的数据中获取2个参数,一个是ID,另一个是ORDER ID。不知道为什么我会得到这个错误。我也在使用打字脚本。
我试过使用req.body,但仍然不起作用。我试过搜索,甚至在实际的nextjs文档上,他们说正确的方法是这样的,而对我来说它不起作用。
console.log(req)的输出

NextRequest [Request] {
  [Symbol(realm)]: {
    settingsObject: { baseUrl: undefined, origin: [Getter], policyContainer: [Object] }
  },
  [Symbol(state)]: {
    method: 'POST',
    localURLsOnly: false,
    unsafeRequest: false,
    body: { stream: undefined, source: null, length: null },
    client: { baseUrl: undefined, origin: [Getter], policyContainer: [Object] },
    reservedClient: null,
    replacesClientId: '',
    window: 'client',
    keepalive: false,
    serviceWorkers: 'all',
    initiator: '',
    destination: '',
    priority: null,
    origin: 'client',
    policyContainer: 'client',
    referrer: 'client',
    referrerPolicy: '',
    mode: 'cors',
    useCORSPreflightFlag: true,
    credentials: 'same-origin',
    useCredentials: false,
    cache: 'default',
    redirect: 'follow',
    integrity: '',
    cryptoGraphicsNonceMetadata: '',
    parserMetadata: '',
    reloadNavigation: false,
    historyNavigation: false,
    userActivation: false,
    taintedOrigin: false,
    redirectCount: 0,
    responseTainting: 'basic',
    preventNoCacheCacheControlHeaderModification: false,
    done: false,
    timingAllowFailed: false,
    headersList: HeadersList {
      cookies: null,
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: [Array]
    },
    urlList: [ [URL] ],
    url: URL {
      href: 'http://localhost:3000/api/callback',
      origin: 'http://localhost:3000',
      protocol: 'http:',
      username: '',
      password: '',
      host: 'localhost:3000',
      hostname: 'localhost',
      port: '3000',
      pathname: '/api/callback',
      search: '',
      searchParams: URLSearchParams {},
      hash: ''
    }
  },
  [Symbol(signal)]: AbortSignal { aborted: false },
  [Symbol(abortController)]: AbortController { signal: AbortSignal { aborted: false } },
  [Symbol(headers)]: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(19) {
      'accept' => [Object],
      'accept-encoding' => [Object],
      'accept-language' => [Object],
      'cache-control' => [Object],
      'connection' => [Object],
      'content-length' => [Object],
      'content-type' => [Object],
      'host' => [Object],
      'origin' => [Object],
      'sec-ch-ua' => [Object],
      'sec-ch-ua-mobile' => [Object],
      'sec-ch-ua-platform' => [Object],
      'sec-fetch-dest' => [Object],
      'sec-fetch-mode' => [Object],
      'sec-fetch-site' => [Object],
      'sec-fetch-user' => [Object],
      'sec-gpc' => [Object],
      'upgrade-insecure-requests' => [Object],
      'user-agent' => [Object]
    },
    [Symbol(headers map sorted)]: [
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array]
    ]
  },
  [Symbol(internal request)]: {
    cookies: RequestCookies { _parsed: Map(0) {}, _headers: [HeadersList] },
    geo: {},
    ip: undefined,
    nextUrl: NextURL { [Symbol(NextURLInternal)]: [Object] },
    url: 'http://localhost:3000/api/callback'
  }
}

a1o7rhls

a1o7rhls1#

SyntaxError: Unexpected token in JSON at position 0:这意味着将请求体解析为await req.json()的JSON失败,因为正如它所说,请求体是无效的JSON语法。这是请求的问题,而不是代码的问题。console.log打印什么?

相关问题