Home > Net >  Discord JS v12 Function Problem (My function is not getting recognized properly)
Discord JS v12 Function Problem (My function is not getting recognized properly)

Time:09-22

I have a problem importing/writing a function which calculates Currency A into Currency B. The problem is that the function is not recognized properly. My Function:

async function convertRMB(inputAmount) {
    const fetch = require("node-fetch");
    try {
        const responseRMB = await fetch('http://www.floatrates.com/daily/cny.json');
        const dataRMB = responseRMB.json();

        const rateEUR = dataRMB.eur.rate
        const rateUSD = dataRMB.usd.rate

        const RMBinEUR = Number(inputAmount) * Number(rateEUR)
        const RMBinUSD = Number(inputAmount) * Number(rateUSD)


        return RMBinEUR;
    } catch (err) { console.log(err) }
}


module.exports = {convertRMB};

The Class im using the function in:

const Discord = require('discord.js');
const convertRMB = require('../functions/convertRMB');

module.exports.run = async (bot, message, args) => {
    var inputAmount = args.join(' ');

    const amountEUR = convertRMB(inputAmount);

    const embed = new Discord.MessageEmbed()
        .setDescription(`${inputAmount}RMB = ${amountEUR}€`)
        .setFooter("© CSGO Library")
    message.channel.send(embed);

    convertRMB(inputAmount);

};

module.exports.help = {
    name: `rmb`
}

The Error im getting: "TypeError: convertRMB is not a function"

CodePudding user response:

As the error says, "convertRMB" is not a function: it is the export object of your file convertRMB.js. You exported the function convertRMB() as a function of module.export, or to be clearer, is the same thing as:

module.export.convertRMB = function (input) {...};

To obtain what you want, that is to export the function as an anonymous one, you should do something like this:

module.exports = function (input) {...};

Or, using your code, you can use the export object that you have:

module.exports.run = async (bot, message, args) => {
    var inputAmount = args.join(' ');

    const amountEUR = convertRMB.convertRMB(inputAmount);

    const embed = new Discord.MessageEmbed()
        .setDescription(`${inputAmount}RMB = ${amountEUR}€`)
        .setFooter("© CSGO Library")
    message.channel.send(embed);

    convertRMB.convertRMB(inputAmount);

};

Here you can find some useful example to better understand the module.export dynamics.

  • Related