#typeorm and mongodb findOneBy issue with find by id, maybe someone have some clues?

1 messages Β· Page 1 of 1 (latest)

stable leaf
#
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);
  }
}
somber crescent
#

{ id: ObjectId(id) }

stable leaf
#

Good it works but It can't any find record

amber eagle
#

FindById So Mongo knows the string is an object Id

stable leaf
#
    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 });
amber eagle
#

just use findById so it gets simpler

stable leaf
#

It doesn't work either

#

One sec

amber eagle
#

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

stable leaf
#

It should work even if I renamed it isn't it?

#
    const userById = await this.usersRespository.findOneById(id);
amber eagle
#

show me your schema please

stable leaf
#
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);
  }
}
amber eagle
#

i dont work with typeOrm but you use that as mongo schema?

#

you dont have a model?

stable leaf
#

Nop typeorm uses entities

#

I also think that it would be better to use models

amber eagle
#

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}````

stable leaf
#

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

amber eagle
#

let me read a bit abt it

#

brb

stable leaf
#

Okay πŸ™

amber eagle
#

quick change you can do is take this out "@Transform(({ value }) => value.toString(), { toPlainOnly: true })"

#
   id: ObjectID;  ```
stable leaf
#

sec

#

Same

amber eagle
#

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

stable leaf
#

Strange it works

amber eagle
#

wdym

stable leaf
#

findByIds works fine

amber eagle
#

is working now right?

#

because you cant find one by id thats not a function im sure

#

xD

stable leaf
#

but findById not working πŸ˜„

amber eagle
#

what πŸ€”

stable leaf
#

Yup

amber eagle
#

so your id is a array?

#

please post the code

#

the query one

stable leaf
#

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

amber eagle
#

Because it’s not findonebyid bro

#

Replace with findById

#

It’s only one when u search by Id

#

That’s naming convention

stable leaf
#

Are you sure

amber eagle
#

Wth isn’t that mongoose?

stable leaf
#

typeorm repository

amber eagle
#

Find({id})

#

Then

stable leaf
amber eagle
#

well, put typeOrm on the bin

#

πŸ˜‚

#

looks like FindOneById is deprecated

#

lets see what did replace it

stable leaf
#

findOneBy()

#

But it doesn't work either :DD

#
findOneBy({ id }}
findOne({ where: { id })

also does not work πŸ˜„

amber eagle
#

"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

stable leaf
#

Tried πŸ˜„

amber eagle
#

id as any πŸ˜‚ (jk dont do it)

stable leaf
amber eagle
#

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

stable leaf
#

Yup

#

But it transforming when serializing into response

#

It shouldn't be the case

amber eagle
#

that transform only occurs on response?

#

console log typof id

stable leaf
#

Yup

amber eagle
#

jesus christ

#

πŸ˜‚

#

where tf he gets the string from

stable leaf
#

Yeah it confusing me :DD

#

String is from cookie

#

parsed jwt token πŸ˜„

amber eagle
#

findOneBy({ id:objectId(id) }}

#

gives you error?

stable leaf
#

Yup

amber eagle
#

or just doesnt return anything?

stable leaf
#

w8 I don't remember which one returns null need to try

#

ObjectId is from typeorm or mongoose?

amber eagle
#

wdym

#

ObjectId is a mongo porperty, idk how it works with typeOrm

stable leaf
#

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

amber eagle
#

can y pls show the db record

stable leaf
#

w8

#

_id

amber eagle
#

boom

stable leaf
#

Fuck it

amber eagle
#

i knew it ahah

amber eagle
stable leaf
#

Sooo response knows that id is _id but find methods don't

#

Wtf πŸ˜„

amber eagle
#

dont ask me

#

πŸ˜‚

stable leaf
#

I need a drink

amber eagle
#

again,thats why i dont use typeOrm

#

πŸ˜‚

stable leaf
#

:DD

#

fu.. it πŸ˜„

amber eagle
#

overComplicating simple things

stable leaf
#

Yeah..

#

Thanks btw πŸ˜„

amber eagle
#

np

#

now imma read react docs cause i cant put my custom schematic working

#

πŸ˜„

stable leaf
#

What you trying to achieve?

#

custom schematic? πŸ˜„

#

What is this πŸ˜„

amber eagle
#

better cli usage

#

to create a new resource with my configs

stable leaf
#

Hmm resource is component, file or what?

amber eagle
#

"module" in nestjs

#

but looks like they use same as react schematic

stable leaf
#

Hmm I am React dev but never integrated it with nestjs πŸ˜„

amber eagle
#

how do you build ur modules? πŸ‘β€πŸ—¨

amber eagle
stable leaf
#

Webpack, vite

#

CRA (also uses webpack)

amber eagle
#

just that looks like it uses the same cli documentation

stable leaf
#

Show me

#

Maybe I'll know something

#

You can write me in private

amber eagle
#

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

#

πŸ˜„

stable leaf
#

You want to create react component with scripts?

amber eagle
#

not a react component

#

a nestJS

#

idk if its called component but its the habit im full stack

#

πŸ˜‚

stable leaf
#

Yeah but it is not react it is nestjs module πŸ˜„

amber eagle
#

i want to modify that one, and looks like the documentation for that is in react ahah

#

ik.. lmao

#

angular i meant

stable leaf
#

Ahh I don't know anything about angular πŸ˜„

amber eagle
#

i know a bit, just hate observables

#

πŸ˜‚

#

react is better