#typescript says me something doesn't exist even if it does (prisma)
58 messages · Page 1 of 1 (latest)
:warning: Please wait a bit longer. You can ping helpers <t:1680213700:R>.
!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.
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
}
})
also, read the error message
i justt sent it
or, right
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
?
i told u what i did
What does the schema look like?
no error when creating the query? and only when accessing the result? 🤔
... though from that screenshot, it kind of looks like role is an empty object and wouldn't have a name property anyway?
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,
}
fixed thanks.
!resolved
bot's down
i need help with one another thing
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
Please do not reping people just because they responded earlier.
just help me and i ll be gone
This is me explaining proper etiquette of the server. Please listen whether I have time to help or not.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
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)
uhh it still gets looped
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." });
}
}