[英](JavaScript) How to return result from another function?
imagem printscreen ---------图像打印屏幕 ---------
Platform: NodeJS平台: NodeJS
I have two roles in the nodejs project I'm having trouble inserting the result of the second function into the first function.我在nodejs项目中有两个角色我无法将第二个function的结果插入第一个function。
How to insert the result of the RUN function within the first function start (client)?如何在第一个 function 启动(客户端)中插入 RUN function 的结果?
function start (client)
...
.sendText (message.from, 'Example' + result.rows + 'Text text')
...
function run ()
...
Console.log (result.rows);
...
'use strict';
const venom = require('venom-bot');
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');
venom
.create()
.then((client) => start(client))
.catch((erro) => { console.log(erro); });
function start(client)
{
client.onMessage((message) =>
{
if (message.body === 'Vendas' && message.isGroupMsg === false)
{ client
.sendText(message.from, 'Example text' + result.rows + 'Etc etc')
.then((result) => {console.log('Result: ', result); })
.catch((erro) => { console.error('Error when sending: ', erro); });
}});
}
//----------------------------------------------------------------
async function run() {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
const sql =`SELECT 'Testeee' FROM dual`;
let result;
result = await connection.execute(sql);
Console.log(result.rows);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
Try making start()
async either and apply wait
.尝试使start()
异步并应用wait
。
async function start(client){
const result = await run();
// the rest of your code
}
In run()
you let the method return the value.在run()
中,您让方法返回值。
async function run() {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
const sql =`SELECT 'Testeee' FROM dual`;
return await connection.execute(sql);
} // rest of your code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.