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

Time:09-06

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 '           
  • Related