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

117
Visualizações
p5.js loadStrings() is doing nothing

I'm using p5.js for my project, and I have to load some data from a file in another folder.

My js code is written in 'js' folder, and the data file is in 'txt' folder. Both 'js' and 'txt' folders are in the same root directory.

Here's my code,

var data;

function preload() {
   data = loadStrings('../txt/data.txt');
   console.log(data);
   console.log(data[0]);
}

and here's the logs:

0: "abcde"
1: "fghij"
2: "klmno"
length: 3
[[Prototype]]: Array(0)

undefined

and here's the request:

Request URL: http://localhost:3000/txt/data.txt
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:3000
Referrer Policy: strict-origin-when-cross-origin

It shows appropriate result when I print whole array, but when I access each data of array, it goes something wrong. What's wrong in my project?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you are calling in the preload, you need to access the data in the setup or draw function.

function setup() {

console.log(data[0])
}

Otherwise, you will need to use a callback function to handle the data.

function setup() {
 loadStrings('../txt/data.txt', myCallback);
}

function myCallback(data) {
console.log(data[0])
}
about 4 years ago · Juan Pablo Isaza 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