如何将'then'回调函数的结果返回给Loopback 4控制器

时间:2019-09-30 13:23:41

标签: typescript typeorm loopback4

我正在尝试在我的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));

  }

我也尝试了以下方法,但是我会知道如何在没有异步/等待的情况下进行管理

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

  }

提前谢谢

2 个答案:

答案 0 :(得分: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));
        });
}

答案 1 :(得分: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 
       })
     );
  }
}