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

Viewed 52

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!

3 Answers

I don't know how your toggle.js constructed, but you can use switch function.

On your toggle.js:

//if your command starts with ${prefix}admin
//Imagine that your command is *admin
//Then call it inside of your module.exports
const connectDB = require('../../utils/connectDB'); // I'm assuming that this is the extension folder. 
module.exports = {
  name: "admin",
  async execute(client, message, args, prefix) {
  //Take note that this is how I call my commands.
      if(args[0] == "connect") {
         switch(this.name) {
            case this.name: connectDB.connectDB(client, message, args, prefix); break;
            default: message.channel.send("SOMETHING WENT WRONG");
         }
      } else if(args[0] == "disconnect")  {
         switch(this.name) {
            case this.name: connectDB.disconnectDB(client, message, args, prefix); break;
            default: message.channel.send("SOMETHING WENT WRONG");
         }
      }
   }
}

Then on your connectDB.js call the function same with the case

async function connectDB(client, message, args, prefix) {
  //your codes here
}

async function disconnectDB(client, message, args, prefix) {
  //your codes here
}

For deep explanation. If you noticed that theres a 2 connectDB.

The first connectDB is your file. then the second connectDB is your function

PS: since you're using interactions. Just remove the message, args, prefix and replace it with interaction

I don't know if this will work with interactions.

I think you can wrap a layer with promise

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)
  }
Related