Hi I’m writing a Twitter bot started from this tutorial: hackermoon.com
For the life of me I can’t get the Twitter data as a global var [followerList] to show outside of the GET method. Will someone give it a quick read? Thank you.
Mutating a global variable from within a promise like this, is also kind of bad practice.
It also looks like you’re trying to use the follower variable as a template, but that’s not going to work because you will be copying by reference, so you’re essentially storing the same variable in every instance of the array. If you want to use a template, you either need to use the object spread operator or Object.assign to copy the object by value before assigning a new variable.
See this fiddle I made for clarification on what’s going to happen if you fix your async error: https://jsfiddle.net/2s3hso3s/
The above code is untested, but it should be closer to what you’re looking for. It doesn’t use your json object template, but you can add that if you feel like you need it. Usually in json you leave keys undefined if they don’t have a value.
Also: Not sure why you’re using Redis here. For a bot, Redis is probably way overkill. Redis is usually used as a shared cache between multiple instances. If you’re trying to cache something, you’re probably going to be better off just using local memory instead of wasting a network call (plus the code complexity) to hit Redis.
Thank you @mawburn. Your code is exactly what I was going for. As for Redis, I’m only using it because it is easy. Do you think I should use mysql database instead?
If you’re looking for persistent data storage, then a database would be better for that anyway. You might also be able to use SQLite or even a .json file, if you don’t want to go for the full MySQL/PostgreSQL setup.
If you’re looking for a cache, you’d be better off with using memory. You don’t need Redis unless you have several of these bots that all need to share a cache.