import { Exclude, Transform } from 'class-transformer';
import { Column, Entity, ObjectId, ObjectIdColumn } from 'typeorm';
@Entity()
export class User {
@ObjectIdColumn()
@Transform(({ value }) => value.toString(), { toPlainOnly: true })
id: ObjectId;
@Column()
email: string;
@Column()
@Exclude()
password: string;
constructor(user?: Partial<User>) {
Object.assign(this, user);
}
}
#typeorm and mongodb findOneBy issue with find by id, maybe someone have some clues?
1 messages Β· Page 1 of 1 (latest)
{ id: ObjectId(id) }
Good it works but It can't any find record
FindById So Mongo knows the string is an object Id
const { id, email } = decoded;
const userById = await this.usersRespository.findOneBy({
id: new Types.ObjectId(id),
});
const userByEmail = await this.usersRespository.findOneBy({
email,
});
console.log({ id, email, userById, userByEmail });
just use findById so it gets simpler
So your collection doesnβt have that Id
π
What is that id u get on find by email
Ur defining it wrong
In ur schema u define as object Iβd, matter of fact Id doesnβt even need to be defined
Why is ur collection id βIdβ and not β_idβ as mongo does it?
Iβll get to pc 2mins
It should work even if I renamed it isn't it?
const userById = await this.usersRespository.findOneById(id);
show me your schema please
import { Exclude, Transform } from 'class-transformer';
import { Column, Entity, ObjectId, ObjectIdColumn } from 'typeorm';
@Entity()
export class User {
@ObjectIdColumn()
@Transform(({ value }) => value.toString(), { toPlainOnly: true })
id: ObjectId;
@Column()
email: string;
@Column()
@Exclude()
password: string;
constructor(user?: Partial<User>) {
Object.assign(this, user);
}
}
delete id from entity
and use "_id"
on your query
export const usersSchema = new mongoose.Schema(
{
email: string
},
{ timestamps: true, versionKey: false }
);```
this is how i would have that schema
mongo creates default _id
then, i use my graphql type ```gql
type User {_id:String, email:String}````
Yeah I know how to use schemas, but I am new with typeorm.. Also in typeorm need to define _id in User entity and I think it will be the same error again
Okay π
quick change you can do is take this out "@Transform(({ value }) => value.toString(), { toPlainOnly: true })"
id: ObjectID; ```
jesus christ now i know why i dont use TypeOrm
π
brb
findOneById(id); make it findByID
when you find by Id is always one, u cant have multiple ids equal
Strange it works
wdym
findByIds works fine
is working now right?
because you cant find one by id thats not a function im sure
xD
but findById not working π
what π€
Yup
It thros
const userByIds = await this.usersRespository.findByIds([id]);
const userById = await this.usersRespository.findOneById(id);
First one works
findOneById throws error
But findByIds works fine π
Lmao
Because itβs not findonebyid bro
Replace with findById
Itβs only one when u search by Id
Thatβs naming convention
Wth isnβt that mongoose?
typeorm repository
well, put typeOrm on the bin
π
looks like FindOneById is deprecated
lets see what did replace it
findOneBy()
But it doesn't work either :DD
findOneBy({ id }}
findOne({ where: { id })
also does not work π
"Repository and EntityManager method .findOneById is deprecated and will be removed in next 0.3.0 version. Use findOne(id) method instead now"
try findOne(id) then
id as any π (jk dont do it)
thats ts problem then, did u took out the other part i told on entity defenition?
this " @Transform(({ value }) => value.toString(), { toPlainOnly: true })"
this is putting the value into string i assume
Yup
or just doesnt return anything?
w8 I don't remember which one returns null need to try
ObjectId is from typeorm or mongoose?
Yeah it's null
const userById = await this.usersRespository.findOneBy({
id: new Types.ObjectId(id),
});
also
const userByIds = await this.usersRespository.findByIds([
new Types.ObjectId(id),
]);
works fine :DD
Gosh
can y pls show the db record
boom
Fuck it
i knew it ahah
I need a drink
overComplicating simple things
Hmm resource is component, file or what?
Hmm I am React dev but never integrated it with nestjs π
how do you build ur modules? πβπ¨
its not integrated
just that looks like it uses the same cli documentation
when u run "nest g res {name}" it creates you a new module "boilerplate" in BE with watever u want, rest,graphql microservices etc, what i want is to do that but with my configs
π
You want to create react component with scripts?
not a react component
a nestJS
idk if its called component but its the habit im full stack
π
Yeah but it is not react it is nestjs module π
i want to modify that one, and looks like the documentation for that is in react ahah
ik.. lmao
angular i meant
Ahh I don't know anything about angular π