0

我正在尝试在我的 loopback4 控制器内的 TypeORM 实体上执行“查找”功能。

问题是,根据 TypeORM 文档,我已经在附加到“createConnection”函数的“then”回调函数中执行了查询。因此,“findOne”调用的结果超出了控制器方法的范围

//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));

  }

我也尝试过以下方法,但我会知道如何在没有 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;

  }

先感谢您

4

2 回答 2

1

试试下面的代码

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));
        });
}
于 2019-09-30T13:49:55.790 回答
0

更清洁的是使用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 
       })
     );
  }
} 
于 2019-11-11T08:50:16.303 回答