Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing undefined value in WHERE condition behaves as true/matching condition in typeorm

I discovered this strange behaviour of typeorm, which is that if the property in the condition of WHERE clause of the FIND query (find, findOne, findBy etc) is undefined, then it behaves like a true condition and returns records (first record for findOne and all records for find).

I know this can be prevented using validators or an IF statement to check against undefined property, but I want to know a direct way with typeorm itself to handle it, apart from this.

Here is what I am doing:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const client = await this.clientRepository.findOne({
        where: {
          id: payload.clientId //undefined
        }
      });

I got the first record of the client table, even though the clientId was undefined.

>Solution :

This is a known issue

The easiest solution using typeORM itself is to use Equal() function of typeORM.

const client = await this.clientRepository.findOne({
        where: {
          id: Equal(payload.clientId)
        }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading