Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
(JavaScript) data.split('\n') is not a function

I am trying to make a random string picker with a text document that splits at every line.

However, when I try to run it, it says that data.split is not a function.

fs.readFile('list.txt', function(err, data){
        if(err) throw err;
        var lines = data.split('\n');
        var rand = [Math.floor(Math.random()*lines.length)];
        var rlist = lines[rand]
})
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

As stated in the documentation:

The callback is passed two arguments (err, data), where data is the contents of the file. If no encoding is specified, then the raw buffer is returned.

A raw buffer is just an array, and JavaScript arrays don't have the split function. So you need to specify an encoding to get the text result.

fs.readFile('list.txt', 'utf8', ...)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda