【问题标题】:How to return the result of a 'then' callback function to the loopback 4 controller如何将“then”回调函数的结果返回到 loopback 4 控制器
【发布时间】:2019-09-30 13:23:41
【问题描述】:

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

  }

提前谢谢你

【问题讨论】:

    标签: typescript typeorm loopback4


    【解决方案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));
            });
    }
    

    【讨论】:

      【解决方案2】:

      使用 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-08-13
        • 2018-11-15
        • 2022-12-09
        • 1970-01-01
        • 2021-11-10
        • 2020-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多