0

I have a function that returns the guilds prefix in Discord.JS:

getprefix.js:

const GuildSchema = require("../Database/Models/GuildConfigs");
const { DEFAULT } = require("./config");

const getprefix = async (id) => {
  const guildConfigs = await GuildSchema.findOne({
    GuildID: id,
  });

  let PREFIX = DEFAULT;

  if (guildConfigs && guildConfigs?.Prefix) {
    PREFIX = guildConfigs?.Prefix;
  }
};

module.exports = { getprefix };

I call the function in another file using this:

let prefix = getprefix(message.guild.id);

prefix.then(() => {
    console.log(prefix);
});

The problem is it returns this in the console:

Promise { '!' }

Is it possible to just return the actual prefix that is inside the quotes with out the Promise?

bong
  • 63
  • 7
  • "Is it possible to just return the actual prefix that is inside the quotes with out the Promise?" No. – Jared Smith Dec 27 '21 at 11:49
  • Does this answer your question? [async/await always returns promise](https://stackoverflow.com/questions/43422932/async-await-always-returns-promise) – Jared Smith Dec 27 '21 at 11:49

2 Answers2

2

Yes, but you must return the value from the async function.

getprefix.js:

const GuildSchema = require("../Database/Models/GuildConfigs");
const { DEFAULT } = require("./config");

const getprefix = async (id) => {
  const guildConfigs = await GuildSchema.findOne({
    GuildID: id,
  });

  let PREFIX = DEFAULT;

  if (guildConfigs && guildConfigs?.Prefix) {
    PREFIX = guildConfigs?.Prefix;
  }
  return PREFIX;
};

module.exports = { getprefix };

and change the call:

let prefix = getprefix(message.guild.id);

prefix.then((value) => {
    console.log(value);
});
user3270865
  • 327
  • 3
  • 9
1

First you must return a value from your async function called getprefix. Secondly you must console.log the result of the promise returned by getprefix function instead of the promise itself :

const getprefix = async (id) => {
  const guildConfigs = await GuildSchema.findOne({GuildID: id});
  
  if (!guildConfigs || !guildConfigs.Prefix) {
    return DEFAULT;
  }

  return guildConfigs.Prefix;
};

getprefix(message.guild.id).then(prefix => console.log(prefix));
Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56