Loop through two documents

I have a collection with a document containing data in an array, i want to loop through this array and get data from each element.

Document A:

{
    "_id": "mthjL06DgJNiwr6lIf",
    "name": "KID1",
    "id": [
        "zAxNarwAE2AnvPb8P",
        "me3AWg85YDvWMShKY",
        "DaX7d98X42zXmlAqW",
        "wZkLkApk857QklApo"
    ],
    "hardware": "5",
}

Document B:

{
    "_id": "zAxNarwAE2AnvPb8P",
    "emails": [
        {
            "address": "hotel@hey.oh",
            "verified": false
        }
    ],        "name": "Jean-Paul"
}

So what i want to do is to loop thourgh the first document A, and with id, i do a research on my database to get emails.adresses and firstname from document B.

At the moment i can get all the emails from the first document don’t know how combine both the task.
to go through the DOCUMENT A and get one by one the senders and get their firstname and emails ?

my code :

  Testg:{
    type: new GraphQLList(Kid),
    resolve: async (_, args, { userId }) => {
      if (!userId) {
        throw new Meteor.Error(401, 'Unauthorized');
      }
      const sendersIterable = []
      const kids = await Kids.find({ "senders.0": userId})
        kids.forEach((kid) => {
          
          sendersIterable.push({
            _id: kid._id,
            name: kid.name,
            email:kid.senders
          })
                for (i of Senders){
      const user =   Meteor.users.findOne({_id: userId },{ emails: 1, profile: 1 })
      return {
        firstname:user.profile.name,
        email:user.emails[0].address
      }
    }
        })
      return sendersIterable
    }
  },

I’d really like to help here, but there is so much wrong with your code that I’m not sure I can decipher what you are actually trying to accomplish.

1 Like

Hello sir, here is my updated code, still i can’t get the emails.adress and name on the Document B, I’m kind new to GraphQl / Meteor

  adminTraps: {
    type: new GraphQLList(Trap),
    resolve: async (_, args, { userId }) => {
      if (!userId) {
        throw new Meteor.Error(401, 'Unauthorized');
      }
      const sendersIterable = []
      const sendersIterable2 = []
      const traps = await Traps.find({ "senders.0": userId })
      traps.forEach((trap) => {
        sendersIterable.push({
          _id: trap._id,
          name: trap.name,
        idSender: trap.senders
        })
        for (let sender in senders){
          const user =  Meteor.users.findOne({ _id: senders[sender] }, { emails: 1, profile: 1 });
          sendersIterable.forEach((user)=>{
            sendersIterable2.push({
              name: user.emails[0].address,
              idSender: senders
          })
    
        })}
      })
      return sendersIterable2
    }
  }