Search code examples
javascriptmongoosediscorddiscord.jsbots

How to return string from then catch lblock inside afunction to another file


connectDB.js

This is the file from where i want to return string...

const mongoose = require('mongoose')
require('dotenv').config()
const DB = process.env.DB

function connectDB(client) {
  if (!DB) return
  mongoose.connect(DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }).then(() => {
    console.log(`${client.user.username} is now connected to database. . .`)
  }).catch((err) => {
    console.log(err)
  })
}

function disconnectDB(client) {
  if (!DB) return
  mongoose.disconnect(DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }).then(() => {
    console.log(`${client.user.username} is now Disconnected to database. . .`)
  }).catch((err) => {
    console.log(err)
  })
}

module.exports = {
  connectDB,
  disconnectDB
}

I want to return a string from then and catch inside connectDB() function as well as from disconnectDB()

Something Like:

function connectDB(client) {
  if (!DB) return
  mongoose.connect(DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }).then(() => {
    console.log(`${client.user.username} is now connected to database. . .`)
    return '🟢 Connected' //If succesfully connects
  }).catch((err) => {
    console.log(err)
    return err.toString() //If some error occures
  })
}

function disconnectDB(client) {
  if (!DB) return
  mongoose.disconnect(DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }).then(() => {
    console.log(`${client.user.username} is now Disconnected to database. . .`)
    return '🔴 Disconnected' //If succesfully disconnects
  }).catch((err) => {
    console.log(err)
    return err.toString() //If some error occures
  })
}

Then I want to access these returned string inside another file:

toggle.js

const {
  connectDB,
  disconnectDB
} = require('../../utils/connectDB')

async execute(interaction, client) {
    const connect = interaction.options.get('connect').value
    var embRes = ''
    var clr = ''

    if (connect) {
      embRes = connectDB(client)
      if (embRes == '🟢 Connected') {
        clr = 'Aqua'
      } else {
        clr = 'Grey'
      }
    } else if (!connect) {
      embRes = disconnectDB(client)
      if (embRes == '🔴 Disconnected') {
        clr = 'Red'
      } else {
        clr = 'NotQuiteBlack'
      }
    }

PLEASE NOTE: that i am in very early stage of development means i don't know much so please explain answer as much as possible!

THANK YOU for the help!


Solution

  • try this

      function connectDB(client) {
        if (!DB) return
        return new Promise((resolve,_) => {
            mongoose.connect(DB, {
                useNewUrlParser: true,
                useUnifiedTopology: true
              }).then(() => {
                console.log(`${client.user.username} is now connected to database. . .`)
                resolve('🟢 Connected' ) //If succesfully connects
              }).catch((err) => {
                console.log(err)
                resolve(err.toString()) //If some error occures
              })
        }) 
      }
    
      async execute(interaction, client) {
        const embRes = await connectDB(client)
      }