#typescript says me something doesn't exist even if it does (prisma)

58 messages · Page 1 of 1 (latest)

lime jewel
#

hellol ook at this

#

!helper

pine flameBOT
#

:warning: Please wait a bit longer. You can ping helpers <t:1680213700:R>.

amber rain
#

!screenshot

pine flameBOT
#
`!screenshot`:

Rather than screenshots, please provide either code formatted as:

```ts
// code here
```
Or even better, as an example on the TypeScript playground that is as simple as possible and reproduces the issue. This makes it easier to help you and increases the chances of getting an answer.

lime jewel
# amber rain !screenshot
    if(!user) {
        res.send({ err: "Couldn't find the user you're looking for." })
    } else {
        const token = await sign({
            id: user.id,
            username: user.username,
            role: user.role.name
        }, process.env.JWT_SECRET_KEY!, { expiresIn: `14d` })
        return token
    }
#
    
    const user = await prisma.user.findUnique({
        where: { username: username },
        include: {
            role
        }
    })
amber rain
#

also, read the error message

lime jewel
#

i did

#

but my users have roles

#

and i use include

amber rain
#

where do you get your user variable from?

#

show the actual query

amber rain
#

or, right

lime jewel
#

see it has role

amber rain
#

do you also have the error when compiling your code manually?

#

or only in your editor?

#

also, have you tried using select instead of include?

#

include is mainly for eager relationships

lime jewel
#

yes

#

i tried

#

include

#

i also did trying manually

#

andit gave that error

amber rain
#

?

lime jewel
cursive summit
#

What does the schema look like?

amber rain
#

no error when creating the query? and only when accessing the result? 🤔

cursive summit
#

... though from that screenshot, it kind of looks like role is an empty object and wouldn't have a name property anyway?

amber rain
#

but

include: {
    role
}

doesn't throw an error 🤔

#

don't you need to set the properties you select to true?

#

so like

include: {
    role: true,
}
sweet junco
#

fixed thanks.

amber rain
#

!resolved

sweet junco
#

!resolved

#

i can't do it

#

LOL

amber rain
#

bot's down

sweet junco
#
import { PrismaClient } from '@prisma/client';
import { NextFunction, Request, Response } from "express";
import { signJWT } from '../middlewares/jwt';
import { hash, verify } from "argon2";

const prisma = new PrismaClient();

export async function signIn(req: Request, res: Response, next: NextFunction) {
  const { email, username, password } = req.body;
  await prisma.$connect();
  
    try {
        const user = await prisma.user.findUnique({
            where: { username }
        })

        if(!user) {
            res.send({
                err: "Couldn't find the user you're looking for."
            })
        }

        if(user) {
            const comparedPass = await verify(user.passwordHash, password)
            if(!comparedPass) {
                res.send({ err: "Invalid Credentials." });
            } else {
                const token = await signJWT(req, res);

                res.send({ message: "Logged In. \n", cookie: token })
            }
        }
    } catch (error) {
        console.log(error);
        res.send({ err: "Unexpected error occured, please try again later." });
    } finally {
        await prisma.$disconnect();
        return;
    }
};

export async function createUser(req: Request, res: Response, next: NextFunction) {
    try {
        await prisma.$connect();

        const { username, email, password } = req.body;

        const userExist = await prisma.user.findFirst({
            where: {
              email,
            },
          });

        if(userExist) {
            return res.send({ err: "User already exists." })

        }
        const hashedPass: string = await hash(password);

        const user = await prisma.user.create({
            data: {
              username,
              email,
              passwordHash: hashedPass,
              role: {
                connect: { name: "USER" }
              }
            },
          });

          
        } catch (error) {
          console.log(error);
          return res.send({ err: "Unexpected error occurred, please try again later." });
        } finally {
          await prisma.$disconnect();
          return;
      }
    }
#

it puts my request in loop

#

it creates the account

#

but request doesn't stop

#

@amber rain

#

@cursive summit

cursive summit
sweet junco
cursive summit
amber rain
#
#

should work as expected

#

maybe just don't open and close the connection to the db that often

#

just open it once and leave it open (the db can handle it just fine)

sweet junco
#
import { NextFunction, Request, Response } from "express";
import { hash, verify } from "argon2";
import { PrismaClient } from "@prisma/client";
import { signJWT } from "../middlewares/jwt";

const prisma = new PrismaClient();

export async function signIn(req: Request, res: Response, next: NextFunction) {
    try {
        const { email, username, password } = req.body;
        const user = await prisma.user.findUnique({ where: { username } });

        if (!user) return res.send({ err: "Couldn't find the user you're looking for." });

        const comparedPass = await verify(user.passwordHash, password);

        if (!comparedPass) return res.send({ err: "Invalid Credentials." });

        const token = await signJWT(req, res);
        return res.send({ message: "Logged In.", cookie: token });
    } catch (error) {
        console.log(error);
        return res.send({ err: "Unexpected error occured, please try again later." });
    }
};

export async function createUser(req: Request, res: Response, next: NextFunction) {
    try {
        const { username, email, password } = req.body;
        const userExist = await prisma.user.findFirst({ where: { email } });

        if (userExist) return res.send({ err: "User already exists." });

        const hashedPass = await hash(password);
        const user = await prisma.user.create({
            data: {
                username,
                email,
                passwordHash: hashedPass,
                role: { connect: { name: "USER" } },
            },
        });
    } catch (error) {
        console.log(error);
        return res.send({ err: "Unexpected error occurred, please try again later." });
    }
}