简体   繁体   中英

returning a variable of an if statement javascript

hi i want to use a variable that i create inside a else statement to use in another function but i cant figure out how

the code >

if (!e){
               await knex('empresa').insert({
                   razao_social,
                   cnpj
               }) 
            }else{
                const e_id = e.id
                console.log(e_id)
                return
            }

i want to get that e_id variable to use in another function but i dont seem to be able to do it

declare the variable in the global scope. And only populate it inside the if statement.

let e_id;

if (!e){
               await knex('empresa').insert({
                   razao_social,
                   cnpj
               }) 
            }else{
                e_id = e.id
                console.log(e_id)
                return
            }

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