Getting this error: `TypeError: this.response.stat...
# support-questions-legacy
p
Getting this error:
TypeError: this.response.status is not a function
, used to work fine but now this.
Stacktrace:
Copy code
TypeError: this.response.status is not a function
    at ExpressResponse.sendJSONResponse (/usr/src/app/node_modules/supertokens-node/lib/build/framework/express/framework.js:152:31)
    at sendNon200Response (/usr/src/app/node_modules/supertokens-node/lib/build/utils.js:135:9)
    at Object.sendNon200ResponseWithMessage (/usr/src/app/node_modules/supertokens-node/lib/build/utils.js:126:5)
    at /usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:75:17
    at Generator.next (<anonymous>)
    at /usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:44:75
    at new Promise (<anonymous>)
    at __awaiter (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:26:16)
    at sendUnauthorisedResponse (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:74:12)
    at Object.<anonymous> (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:187:30)
    at Generator.next (<anonymous>)
    at /usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:44:75
    at new Promise (<anonymous>)
    at __awaiter (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:26:16)
    at Object.onUnauthorised (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/utils.js:186:13)
    at SessionRecipe.<anonymous> (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/recipe.js:147:64)
    at Generator.next (<anonymous>)
    at /usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/recipe.js:44:75
    at new Promise (<anonymous>)
    at __awaiter (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/recipe.js:26:16)
    at SessionRecipe.handleError (/usr/src/app/node_modules/supertokens-node/lib/build/recipe/session/recipe.js:135:13)
    at SuperTokens.<anonymous> (/usr/src/app/node_modules/supertokens-node/lib/build/supertokens.js:336:64)
r
Are you manually sending a custom response from an api override? Can you share the code please?
p
I've made a lot of API overrides, my backend config is in the attached file
r
Also, when you say that it used to work fine, but isn’t now - is that cause you did a version update to the backend SDK? Or just updated your code?
p
I didn't touch it for weeks, I tried updating the SDK to fix it but that didn't fix it. It wasn't caused by an update to the backend SDK. But NPM might have decided to autoupdate it, not sure. But it errors inside the supertokens-node library when attempting to send a JSON response
r
Which part of the file you shared does the error come from?
p
I'm not sure, the stacktrace doesn't give any clue about where the error comes from but its alteast in Supertokens-node's error handeling. I'll take a look in the logging system to see if I can find something there
r
Which version of express are you using?
Also, how have you added the supertokens middleware to your app? Can you show me that part too?
Finally, does this happen for all API calls related to auth that are fired from the frontend? Or just one - if one, then which one?
p
NextJS ^12.2.5
Copy code
ts
export async function getServerSideProps(context: GetServerSidePropsContext) {
    await superTokensNextWrapper(
        async (next) => {
            await verifySession({ sessionRequired: true })(context.req as SessionRequest, context.res as any, next);
        },
        context.req,
        context.res
    )

    let session = (context.req as SessionRequest).session;

    if (session === undefined) throw new Error(`ufkc`)

    let userId = session.getUserId();
r
I’m talking about the supertokens middleware. Not the verifySession function
p
I'll check that once I can login to my app, i'm locked out at the moment
I'm not sure where that should be placed in my NextJS project, its been quite a while since I touched it
r
The api folder -> the /auth/* route
p
Ah, the contents of that file is:
Copy code
ts
import { superTokensNextWrapper } from 'supertokens-node/nextjs'
import { middleware } from 'supertokens-node/framework/express'
import { NextApiRequest, NextApiResponse } from 'next'
import { Request, Response } from 'express';
import supertokens from 'supertokens-node'
import { backendConfig } from '../../../config/backendConfig'
import NextCors from "nextjs-cors";

supertokens.init(backendConfig())

export default async function superTokens(
    req: NextApiRequest & Request,
    res: NextApiResponse & Response
) {

    let origin = *origin mess here*

    // NOTE: We need CORS only if we are querying the APIs from a different origin
    await NextCors(req, res, {
        methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
        origin,
        credentials: true,
        allowedHeaders: ["content-type", ...supertokens.getAllCORSHeaders()],
    });

    await superTokensNextWrapper(
        async (next) => {
            await middleware()(req, res, next)
        },
        req,
        res
    )
    //if (!res.writableEnded) {
    //    res.status(404).send('Not found')
    //}
}
r
Ok thanks. I’ll wait for this answer.
p
When logging requests with Safari devtools the only red items I see are to my own API endpoind, contents being:
Copy code
ts
import { superTokensNextWrapper } from 'supertokens-node/nextjs'
import { SessionRequest } from 'supertokens-node/framework/express'
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { NextApiRequest, NextApiResponse } from 'next'
import { Request, Response } from 'express';
import supertokens from 'supertokens-node'
import { backendConfig } from '../../config/backendConfig'
import NextCors from "nextjs-cors";
import UserMetadata from 'supertokens-node/recipe/usermetadata'

supertokens.init(backendConfig())

export default async function userData(
    req: NextApiRequest & Request,
    res: NextApiResponse & Response
) {

    await superTokensNextWrapper(
        async (next) => {
            await verifySession()(req, res, next)
        },
        req,
        res
    )

    const session = (req as SessionRequest).session;

    if (session == undefined) {
        res.status(401).send("Unauthorized")
        return
    }
    const userId = session!.getUserId();

    const { metadata } = await UserMetadata.getUserMetadata(userId);

    res.json(metadata)

}
But Safari being Safari it didn't log any response info, I'll try Chrome
r
Ahh I see. So that auth APIs and overrides work
Just your APIs that use verifySession fail?
p
I think so. When logging in I do get sent back to the login screen
r
So you get back a 401 from the api? Or a 500?
p
401
r
So you get a 401 and the error stack you sent above? (About the function not found)
p
Yes
r
Does the 401 result in calling the refresh api from the frontend and that causes the error stack above?
p
After the failed API call it goes directly to the login page without any other endpoints called
r
Can you do a console log in your api right before the const session = …. line? Does that log get printed out?
Also, could I see the request and response headers for the userdata api ?
Finally, it would be great if you can open an issue about this in our node repo referencing this conversation.
Along with the exact versions being used (see in yarn.lock file) of the supertokens-node and NextJS lib
p
It does reach
if (session == undefined) {
, but after that it just fails and the client redirects to login (which is my code).
r
Hmm. This whole thing is very strange. I’m not sure what the issue is
The error stack indicates that the res object that you pass to verifySession doesn’t have the status() function. But you are using that function right below it and that does work.
p
Yes, using the VSCode debugger I've found this error:
Exception has occurred: Error: Session does not exist. Are you sending the session tokens in the request with the appropriate token transfer method
during login. And the request to my userdata api endpoint doesn't have any Cookies or other data attached to it
r
So that’s another problem. But first we need to figure out why that function not found stack trace is showing up at all.
It’s really late here (3 am) so I’ll reply tomorrow, but please open an issue about this on our GitHub so that I don’t forget. Thanks
p
I'll open it tommorow as its also quite late here. Good night
r
ok thanks!
18 Views