0

activeUsers - array of mongoose objects activeUsers.campaings - array of mongoose objects landingsFromBinom - array of objects where store id [{id: 123, name: name}, {id: 133, name: name2}]

For each user i have campaigns array (mongoose doc), for each campaigns i have langings array (mongoose doc).

I fetch all users from db and start iterate for each. For each campaign i fetch 'landings' and start iterate for each.

Look at db and compare id's that i need to change and store it value in "landingsIds"

PROBLEM: when i console log "data" that returns me empty array, but if i console.log in function that return filled array. How to fix it?

 // Fetch users from db
  let activeUsers = await User.find().populate({
    path: 'campaigns',
    match: { active: true },
  })

  const data = activeUsers.reduce((results, user) => {
    // For each campaign get landings
    user.campaigns.forEach(async (campaign) => {
      const { landings } = await campaign.populate({
        path: 'landings',
        match: { active: true },
      })

      // Get only good ID
      const landingsIds = landings.reduce((results, landing) => {
        landingsFromBinom.forEach((binomLanding) => {
          if (
            parseInt(binomLanding.id) === landing.id &&
            binomLanding.inject >= landing.todayVisitors
          ) {
            results.push(landing.id)
          }
        })
        return results
      }, [])

      results.push({
        landings: landingsIds,
        user: user._id
      })
      // RETURN Filled array
      console.log(results)
    })
    return results
  }, [])

  // Return empty array
  console.log(data)

Output :

[]
[
  {
    landings: [ 3, 11, 1, 7 ],
    user: new ObjectId("617fb23b1e7ea187ae07a6ee")
  }
]

1 Answer 1

1

use await in array.reduce with Promise.resolve

let activeUsers = await User.find().populate({
    path: 'campaigns',
    match: { active: true },
  })

  const data = await activeUsers.reduce(async (results, user) => {
    // For each campaign get landings
    user.campaigns.forEach(async (campaign) => {
      const { landings } = await campaign.populate({
        path: 'landings',
        match: { active: true },
      })

      // Get only good ID
      const landingsIds = landings.reduce((results, landing) => {
        landingsFromBinom.forEach((binomLanding) => {
          if (
            parseInt(binomLanding.id) === landing.id &&
            binomLanding.inject >= landing.todayVisitors
          ) {
            results.push(landing.id)
          }
        })
        return results
      }, [])

      results.push({
        landings: landingsIds,
        user: user._id
      })
      // RETURN Filled array
      console.log(results)
    })
   
  }, [], Promise.resolve(results))

  console.log(data)
Sign up to request clarification or add additional context in comments.

2 Comments

returned undefined
Try again i update my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.