-1

Im trying to export prefix from my "prefix.js" to "index.js" and I want it to return "?" becuse the prefix on the database is "?"

This is my code for "prefix.js"

const GuildConfig = require("../database/schemas/GuildConfig");
const { Client } = require("discord.js");
const client = new Client();

const prefix = () => {
  if (message.author.bot) return;
  const guildConfig = GuildConfig.findOne({
    guildId: message.guild.id,
  });
  const prefix = guildConfig.get("prefix");Z
  return prefix
};

exports.prefix = prefix

And this is some of the code from "index.js"

const prefix = require("./src/events/prefix")

console.log(prefix)

And this is what I get in the console

{ prefix: [Function: prefix] }

BenjiCS
  • 1
  • 3
  • 2
    It's because the `prefix` export isn't the _default export_, so you'll have to either access it through [_object destructuring_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) or by `prefix.prefix`. Alternatively, you should instead use `module.exports` - more info about it can be found in this [StackOverflow question](https://stackoverflow.com/q/5311334). – Edric Sep 03 '20 at 19:46

2 Answers2

0

You can tie prefix to export when you declare the function earlier or export it later by using {} braces.

  1. export function prefix() {}

OR

  1. prefix = () => {}

export {prefix}

Here's an article that clears up export/import a bit: https://javascript.info/import-export

0

Answer

What you have to do is call your prefix function as the following:

Option 1 (By deconstructing your object)

const { prefix } = require("./src/events/prefix")
console.log(prefix())

Option 2 (By calling your function)

const prefix = require("./src/events/prefix")
console.log(prefix.prefix())

Reference

NodeJS exports

Jose Vasquez
  • 1,678
  • 1
  • 6
  • 14