0

I'm trying to perform a 'find' function on a TypeORM Entity inside my loopback4 controller.

The problem is that according to TypeORM documentation I've executed the query in the 'then' callback function attached to the 'createConnection' function. Thus, the result of 'findOne' call is out of the scope of the controller method

//this is the controller method signature
async findById(@param.path.number('id') id: number): Promise<Pratica> {

    createConnection({
      type: 'oracle',
      host: '10.64.2.226',
      port: 1521,
      sid: 'NPRA02S',
      username: 'sua03e',
      password: 'iniziale',
      entities: [
        Pratica
      ],
      logging: true
    }).then(async connection => {
      let praticaRepository = connection.getRepository(Pratica);

      // I have to return this as a result parameter in the controller
      let pratica = await praticaRepository.findOne({ protocolloaci: id });

    }).catch(error => console.log(error));

  }

i've tried also the following, but I would know how to manage the same without async/await

//this is the controller method signature
async findById(@param.path.number('id') id: number): Promise<Pratica> {

    let connection = await createConnection({
      type: 'oracle',
      host: '10.64.2.226',
      port: 1521,
      sid: 'NPRA02S',
      username: 'sua03e',
      password: 'iniziale',
      entities: [
        Pratica
      ],
      logging: true
    })

    let praticaRepository = connection.getRepository(Pratica);

    return await praticaRepository.findOne({ protocolloaci: id }) || new Pratica;

  }

Thank you in advance

Yanosh
  • 368
  • 5
  • 15

2 Answers2

1

Try below code

async findById(@param.path.number('id') id: number): Promise<Pratica> {

        return new Promise( (resolve)=>{

            createConnection({
              type: 'oracle',
              host: '10.64.2.226',
              port: 1521,
              sid: 'NPRA02S',
              username: 'sua03e',
              password: 'iniziale',
              entities: [
                Pratica
              ],
              logging: true
            }).then(async connection => {
              let praticaRepository = connection.getRepository(Pratica);

              // I have to return this as a result parameter in the controller
              const pratica = await praticaRepository.findOne({ protocolloaci: id });
              resolve(pratica); // return using resolve
            }).catch(error => console.log(error));
        });
}
hrdkisback
  • 898
  • 8
  • 19
0

More cleaner would be to use Promise.resolve:

async findById(@param.path.number('id') id: number): Promise<Pratica> {
  try {

     const connection = **await** createConnection({
       type: 'oracle',
       host: '10.64.2.226',
       port: 1521,
       sid: 'NPRA02S',
       username: 'sua03e',
       password: 'iniziale',
       entities: [
        Pratica
       ],
       logging: true
     });

     let praticaRepository = connection.getRepository(Pratica);

     const pratica = await praticaRepository.findOne({ 
       protocolloaci: id 
     });

     return **Promise.resolve**(
        this.response.status(200).send(pratica)
     );

  } catch(err) {
     console.log(error.message);
     return Promise.resolve(
       this.response.status(400).send({ 
         error: err.message, 
         status: 400 
       })
     );
  }
} 
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72