我正在尝试在我的 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;
}
先感谢您