Array .push not working

I have an array specified in my code which I want to use .push on whenever a specific command is called. However this .push is not working, is there something specific I’m unaware of that is making me unable to use this?

EDIT (my reply to my own post is still awaiting approval even a full day after submitting it)
@househaunt Here’s the link: [redacted]

It’s supposed to be a twitch bot, and when a specific command is called from chat it should use .push on one of the arrays, but when I check the arrays afterwards with a different command I made for that the .push doesn’t appear to have done anything

Hi @Urgaan! Welcome to the Glitch forum! Can you share the code (or a link to your project?) Sounds like an interesting bug!

lol, oops. wrong thread

Hey @Urgaan - I edited your post, and your other replies ended up rejected because you are including a share link that opens up any user to go into your project. If you’d like to send a join link to someone you trust, I recommend you send them a private message!

1 Like

@jenn Ah ok, thank you very much. I am new to Glitch so I was unaware of this.

I’ll post the code here in that case, should be easier to keep track of in case someone else has the same issue at some point.

if(commandName[0] === "!add"){
    //If there is an actual parameter used in the command
    if(commandName.length > 1){
      //Push the parameter
      quotes.push(commandName[1]);
  
      //Return a confirmation message
      client.say(target, "Quote added");
    } else {
      //Return a denial message if no parameter is used
      client.say(target, "Invalid arguments");
    }
}

Interesting – do you get an error message (such as Uncaught TypeError: quotes.push is not a function or Uncaught ReferenceError: quotes is not defined) in the console when you run the code? My guess would be either that somehow quotes is not an array, or you don’t have a reference to quotes in this code block, or…somehow those changes are updating the object that you expect (outside of the block?) If your project is public, you can just send me the name, and I can take a look at the code in context? Or DM me a collab link if you want!

1 Like

I think the project name is: organized-leaf.

I did some more testing and the .push does work, but it’s a bit tricky. Since the array is stored in the same .js file and has some hardcoded quotes in it, the change added by .push is removed whenever the bot is loaded/changed.

I have joined the project which you mentioned, and took a look at what you were doing, I noticed that with the way you had your array and code setup, the quotes that you added to the array, would end up resetting every time the project restarted, so I moved it over to an array using quick.db, I tested everything, and made sure it worked, and your error is now resolved!


I stored all the quotes you had, into the quick.db array, so you do not need to worry about that!

2 Likes

This post was flagged by the community and is temporarily hidden.

Thank you very much @Callum-OKane!

Quick question, where can I find quick.db? I can’t seem to find it in the files of the project

You cannot access the storage via files, you will have to use db.fetch("whatever.here")
to fetch the quotes array that I have created for you, just use db.fetch("quotes"). You can interact with this json array like any other array or json file.

1 Like