Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
forEach javascript

I have this fetchTweets function which is called with cron every 10secs, and my problem is that newTweets variable is not incrementing, in console i am getting 0. As you can see i initialized variable and gave it initial value 0, and its being incremented in forEach and then returned.

fetchTweets: async () => {
        let dt = dateTime.create()
        const time = dt.format('Y-m-d H:M:S')

        let users = await userControl.getAllUsers('WHERE isActive = ?', 1)

        let twitterUsers = Object.values(JSON.parse(JSON.stringify(users)))
    
        var newTweets = 0
        twitterUsers.forEach(async (user) => {
            twitterResponse = await twitter.get(`users/${user.twitter_id}/tweets`, { max_results: 10 })

            twitterResponse.data.forEach(async (tweet) => {
                const tweetCheck = await SQL('SELECT COUNT(id) as cn FROM twitter_tweets WHERE tweet_id = ?', [tweet.id])
                if(tweetCheck[0].cn == 0) {
                    newTweets++
                    const t = [user.twitter_id, tweet.id, tweet.text, time]
                    await SQL('INSERT INTO twitter_tweets SET twitter_id = ?, tweet_id = ?, tweet_text = ?, added_at = ?', t)
                }
            })
        })
        return newTweets
    }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!