Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 699

`headers` was called outside a request scope

$
0
0

Im trying to create a temporary session in my API using nextJS version 14.2.3, this session needs to be created using a API-KEY and Bearer Token comming from the headers, but im getting this error, heres the code im trying to use:

`import { ApiError } from '@/lib/errors';import { throwIfNoAccessToApiKey } from '@/lib/guards/team-api-key'; // Adjust the import path based       on your project structureimport { throwIfNoTeamAccess } from 'models/team';import { getOrders } from 'models/order';import type { NextApiRequest, NextApiResponse } from 'next';import { getServerSession } from 'next-auth';import NextAuth  from 'next-auth';import { getAuthOptions } from '@/lib/nextAuth';export default async function handler(req: NextApiRequest, res: NextApiResponse) {  try {    const session = await getServerSession(getAuthOptions(req, res));    console.log("SESSION: ", session)    const { teamId } = await throwIfNoTeamAccess(req, res);    console.log("TEAM ID: ", teamId)    switch (req.method) {      case 'GET':        await handleGET(req, res, teamId);        break;      default:        res.setHeader('Allow', 'GET, POST, DELETE, PATCH');        res.status(405).end(`Method ${req.method} Not Allowed`);    }  } catch (error: any) {    const message = error.message || 'Something went wrong';    const status = error.status || 500;    res.status(status).json({ error: { message } });  }}`

Viewing all articles
Browse latest Browse all 699

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>