#res.
1 messages · Page 1 of 1 (latest)
The error message you're seeing, TypeError: res.status is not a function, typically indicates that the res object you're trying to use does not have a status method. This could be due to a few reasons:
- The
resobject is not correctly passed into your function. Ensure that your function signature correctly receives thereq(request) andres(response) objects. Here's an example from the Next.js documentation (https://nextjs.org/docs/pages/building-your-application/routing/api-routes):
export default function handler(req, res) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
-
You're trying to use Express.js-like helper methods in a context where they are not available. In Next.js, these helper methods are available in API Routes, as described in the Next.js documentation (https://nextjs.org/docs/pages/building-your-application/routing/api-routes). If you're not in an API Route, these methods may not be available.
-
You're using a custom server or a different framework that does not provide these helper methods on the
resobject.
Without more context or code, it's hard to provide a more specific solution. Please ensure that you're using the res object in the correct context and that it's being passed into your function correctly.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@glacial pecan kapa.ai is still learning and improving, please let us know how it did by reacting below
@dusk goblet import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next';
import { cookies } from 'next/headers'; // Unused import
import { logger } from '@utils/logger';
interface ResponseObject {
status: number;
body: {
error?: string;
};
}
export const GET = async (
req: NextApiRequest,
res: NextApiResponse<{ error?: string }>
) => {
logger.log('Full request URL:', req.url);
const queryParams = new URLSearchParams(req.url?.split('?')[1] || '');
const code = queryParams.get('code');
if (!code) {
logger.error('No code provided in the query.');
const errorResponse: ResponseObject = {
status: 400,
body: { error: 'No code provided in the query.' }
};
return res.status(errorResponse.status).json(errorResponse.body);
}
// Handle possible string array for req.query.error and req.query.error_description
if (req.query?.error) {
const errorDescription = typeof req.query.error_description === 'string' ? req.query.error_description : undefined;
const error = typeof req.query.error === 'string' ? req.query.error : undefined;
logger.error('Discord OAuth error:', errorDescription || error);
const errorResponse: ResponseObject = {
status: 400,
body: { error: errorDescription || error }
};
return res.status(errorResponse.status).json(errorResponse.body);
}
logger.log('Extracted code:', code);
try {
const oauthResult = await axios.post(
'https://discord.com/api/oauth2/token',
new URLSearchParams({
client_id: process.env.DISCORD_CLIENT_ID!,
client_secret: process.env.DISCORD_CLIENT_SECRET!,
code,
grant_type: 'authorization_code',
redirect_uri: process.env.DISCORD_REDIRECT_URI!,
scope: 'identify guilds'
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
const token: string = oauthResult.data.access_token;
logger.log('Received token from Discord:', token);
const successResponse: ResponseObject = {
status: 302,
body: {}
};
res.status(successResponse.status).setHeader('Location', process.env.DASHBOARD_URL!).end();
} catch (error: any) {
logger.error('OAuth Error:', error.response?.data || error.message);
const errorResponse: ResponseObject = {
status: 500,
body: { error: 'Internal server error.' }
};
return res.status(errorResponse.status).json(errorResponse.body);
}
};
Let me check the knowledge base for you. One moment! :books:
The code you've shared is a Next.js API route handler for a GET request. It's designed to handle OAuth authentication with Discord. Here's a breakdown of what it does:
- It logs the full request URL.
- It extracts the
codeparameter from the query string. If nocodeis provided, it logs an error and sends a400status response with an error message. - If there's an error in the query parameters, it logs the error and sends a
400status response with the error description. - If the
codeis present and there are no errors in the query parameters, it makes aPOSTrequest to the Discord OAuth2 token endpoint to exchange thecodefor an access token. - If the request to Discord is successful, it logs the received token and sends a
302status response, redirecting the client to a dashboard URL. - If there's an error in the request to Discord, it logs the error and sends a
500status response with an error message.
This code uses several features of Next.js and Node.js, including:
- The
NextApiRequestandNextApiResponsetypes from Next.js, which are used to type thereq(request) andres(response) objects. - The
axioslibrary to make HTTP requests. - The
URLSearchParamsinterface to work with the query string of the URL. - Environment variables (like
process.env.DISCORD_CLIENT_ID) to securely store and access sensitive information. - The
res.status().json()andres.status().setHeader().end()chains from Next.js to send responses with specific HTTP status codes, JSON bodies, and headers.