Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
Check if user has a data for Economy Bot discord.js

Heyo, I'm coding on an Economy-System right now but I have a problem. I have to check if the user has data and if he doesn't have data create it. Here is what I've done:

if (UserJSON.user[message.author.id]) { //It doesn't detect the message.author.id
  console.log("User Data already exists");
  return;
} else {
    const newUser = {
      user: message.author.id,
      stats: {
        money: 0
      }  
    }
    UserJSON.push(newUser);
    fs.writeFileSync("./data/users.json", JSON.stringify(UserJSON, null, 2));
}

The writing data function works perfectly as you can see down here in the users.json

[
  {
    "user": "654022997983756328",
    "stats": {
      "money": 0
    }
  },
  {
    "user": "917065319724507176",
    "stats": {
      "money": 0
    }
  }
]

The only problem is "check if the user has data" It doesn't detect

[message.author.id]

Someone told me to do

if (UserJSON.[message.author.id].user) {

but sadly it didn't work too. Can anyone help me here fix this issue?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

That's because UserJSON is an array and you're trying to access the "user" prop in the if condition, which doesn't exist. You need to check if the UserJSON array contains an object which has "user" value of the condition. Try this: if (UserJSON.find((userObject) => parseInt(userObject.user) === message.author.id)) { console.log("User Data already exists"); return; }

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda