Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Trying to use replace() method on files by passing the string not working - JS

I'm trying to replace a string, but when i try to do it on a file, it does not work. Or only the last file will work.

Main Code (inside a class method):

const funcs = fs.readdirSync(path.join(__dirname, "../funcs")).filter(file => file.endsWith('.js'));
        
this.functions = this.code.split("$");

const functions = this.functions

for (const func of funcs) {
    for (let x = functions.length - 1; x > 0; x--) {
         let i = 0;
         const res = await require(`../funcs/${func}`)(client, this.code.toString(), this._author);

         console.log(x);
         console.log(res);
         console.log(functions);

         return res;
         i++;
    }
}

ping.js:

const ping = (client, code, author) => {
    const result = code.split("$[ping]").join(client.ws.ping)

   return result;
}

module.exports = ping;

messageAuthorTag.js:

const Util = require('../utils/util');

const messageAuthorTag = (client, code, author) => {
    if (code === null) return;
    
    const res = code.split("$[message.author.tag]").join(`${author.tag}`);

    return res;
}

module.exports = messageAuthorTag;

Using it : "ping: $[ping] author tag: $[message.author.tag]"

Output: ping: $[ping] author tag: Example#0001

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your issue is that you return res in the middle of a for loop. This means it won't do anything else in the function and will skip the rest of the loops (like running the other functions!)

As an additional note, you could also remove the let i = 0 and i++ since that doesn't seem to be used for anything.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!