如何 return 'then' 回调函数的结果到环回 4 控制器
How to return the result of a 'then' callback function to the loopback 4 controller
我正在尝试对我的 loopback4 控制器内的 TypeORM 实体执行 'find' 函数。
问题是,根据 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;
}
提前致谢
试试下面的代码
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));
});
}
更清洁的方法是使用 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
})
);
}
}
我正在尝试对我的 loopback4 控制器内的 TypeORM 实体执行 'find' 函数。
问题是,根据 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;
}
提前致谢
试试下面的代码
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));
});
}
更清洁的方法是使用 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
})
);
}
}