Home > Software design >  I want Typeorm to return undefined rather than an error
I want Typeorm to return undefined rather than an error

Time:07-27

Hi I want to check if a user is in the database already.

I use Typeorm and Postgres.

I use Typeorm to check if the user is in my DB if not so that's ok and I want the variable to be null/undefined. The issue is that it stops my server with a 400 error code and the process stops.

const isInDb = await this.clientDb.findOne({ email });

error when not found response: { statusCode: 400, message: 'cannot update DB, this request is invalid', error: 'Bad Request' },

CodePudding user response:

I'm not familiar with the TypeORM API, but if you want to replace unwanted rejections, you could always wrap the returned promise:

function hideRejection<T>(promise: Promise<T>, rejectValue: T = undefined): Promise<T> {
  return promise.catch(() => rejectValue)
}

Then use it like:

const isInDb = await hideRejection(this.clientDb.findOne({ email }));

CodePudding user response:

I just used getcount() query builder to check how many times the row exists. This doesn't throw an error.

  • Related