简体   繁体   中英

(JavaScript) How to return result from another function?

imagem printscreen ---------

Platform: NodeJS

I have two roles in the nodejs project I'm having trouble inserting the result of the second function into the first function.

How to insert the result of the RUN function within the first function start (client)?

function start (client)
...
.sendText (message.from, 'Example' + result.rows + 'Text text')
...


function run ()
...
Console.log (result.rows);
...

Full code

'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 .

async function start(client){
    const result = await run();

    // the rest of your code
}

In run() you let the method return the value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM