0

For some reason when I try and call my command it returns [object Object],[object, Object]

fs.readFile(path.join(__dirname, "../moderation") + "/modlogs.json", "utf-8", function(err, data) { // read the JSON file
  if (err) throw err; // throw error if applicable

var arrayOfObjects = JSON.parse(data); // parse the data

for (let i = 0; i < arrayOfObjects.warns.length; i++) { // loop through all keys in warns file
  if (arrayOfObjects.warns[i].user_id === user.id) { // check if the user has already been warned
    message.reply("User already warned. Kicking user."); // display kick
    //message.guild.member(user).kick(); // kicks member
    indexOfUser = arrayOfObjects.warns.findIndex(x => x.user_id == user.id); // find the index of the users object
    //message.channel.sendMessage(indexOfUser);
    message.channel.sendMessage("Before splicing" + arrayOfObjects.warns);
    //arrayOfObjects.warns.splice(indexOfUser, 1); // remove the user from warns array
    message.channel.sendMessage("After splicing" + arrayOfObjects.warns);
    return;
  };
};

The line //arrayOfObjects.warns.splice(indexOfUser, 1); // remove the user from warns array is supposed to delete that object from the warns array in my JSON file. However it doesn't, the console.logs were just to see what was getting outputed, and it seems like the values aren't getting through.

1 Answers1

0

I think the problem is that you are using findIndex() instead indexOf() when you try to find the index.

Array.prototype.indexOf() expects a value as first parameter. This makes it a good choice to find the index in arrays of primitive types.

Array.prototype.findIndex() expects a callback as first parameter. Use this if you need the index in arrays with non-primitive types (e.g. objects) or your find condition is more complex than just a value.

See the links for examples of both cases. info from this post

Edit:

I bring you some usefull code.

With indexOf()

  if (arrayOfObjects.warns.indexOf(user_id) > -1) { // check if the user has already been warned

    message.reply("User already warned. Kicking user."); // display kick
    //message.guild.member(user).kick(); // kicks member

    indexOfUser = arrayOfObjects.warns.indexOf(user_id);

    //message.channel.sendMessage(indexOfUser);
    message.channel.sendMessage("Before splicing" + arrayOfObjects.warns);
    //arrayOfObjects.warns.splice(indexOfUser, 1); // remove the user from warns array
    message.channel.sendMessage("After splicing" + arrayOfObjects.warns);
    return;
  };

Aclaration: indexOf(value) returns -1 if it can't find the item in the Array. Otherwise it returns the index where item is located. So you don't need to iterate in the array.

with findIndex()

When using findIndex() you don't define the value you want to find in the array. You define the function that will be executed every iteration.

You could do something like:

function iswarned (elem) {
 return elem.user_id == user_id;
}

 if (arrayOfObjects.warns.findIndex(iswarned) > -1) {
   indexOfUser = arrayOfObjects.warns.findIndex(iswarned);
}

Aclaration: findIndex() returns the first index for what callback function returns a truthy value or -1 If the callback never returns a truthy value or array.length is 0.

Community
  • 1
  • 1
Sam
  • 1,459
  • 1
  • 18
  • 32