Find for every name in array some user data out of another collection in Meteor

Hi Guys I am trying to get for every name in the array “eingeladene” the email adress the “Vorname” and the “Nachname” I have a for loop to go over again and again but my find is outputting “null” ;( so my find is not working correctly I have two collections one is named “Events” where the array is inside and in the collection “Benutzer” is the User Data i want to get.

In the array the “Vorname” is inserted so I am searching for the user with this name (“Vorname”) and than get the email + the “Nachname”

for (var i in eingeladene) {

    var email = Benutzer.findOne({"Vorname": eingeladene[i]}, {fields: {email: 1}});
    var Vorname = Benutzer.findOne({"Vorname": eingeladene[i]}, {fields: {Vorname: 1}});
    var Nachname = Benutzer.findOne({"Vorname": eingeladene[i]}, {fields: {Nachname: 1}});
*/

    if (! email) {
        continue;
    }

    var emailData = {
        Name: Name,
        Beschreibung: Beschreibung,
        erstelltAm: new Date(),
        Datum: Datum,
        Eingeladen: eingeladene,
        Autor: Autor,
        Einladender: Einladender
    };

    Email.send({
        to: "email",
        from: "example@gmail.com",
        subject: "Einladung",
        html: SSR.render('EinladungsEmail', emailData),
    });

} 

Thank you for every help and yes I already red the documentation By the way @iiro helped me to get so far

Update: I tried with the same code and to check the generated data I inserted the data in a different collection and I was wrong it is not “null” i now get this:

{
  "_id": "HDF3XnvKzovnNdEiW",

  "Email": {
    "_id": "Gkddi6KhYf6BmF3jv",
    "email": "examplemail@gmail.com"
  },
  "Vorname": {
    "_id": "Gkddi6KhYf6BmF3jv",
    "Vorname": "exampleVorname"
  },
  "Nachname": {
    "_id": "Gkddi6KhYf6BmF3jv",
    "Nachname": "exampleNachname"
  }
} 

The Problem with this is that for every part the id is inserted with the data

My goal would be this

"Email": "examplemail@gmail.com"

Hope you can help me now 😉

Answer

One thing I’m noticing is wrong is your syntax for findOne.

You don’t specify ‘fields’ in the object.

If you want just email:

Benutzer.findOne({Vorname: eingeladene[i]}, {email: 1});

https://docs.mongodb.com/manual/reference/method/db.collection.findOne/


Edit: Sorry just realized this isn’t a very efficient way of doing what you’re trying to do and you have some syntax issues.

A better solution with the same outcome:

var obj = Benutzer.findOne({'vorname': eingeladene[i]}, {});

var emailData = {
    name: obj.name,
    beschreibung: obj.beschreibung,
    erstelltAm: new Date(),
    datum: obj.datum,
    eingeladen: obj.eingeladene,
    autor: obj.autor,
    einladender: obj.einladender
};

Email.send({
    to: obj.email,
    from: "example@gmail.com",
    ...
});

Making a different MongoDB is insanely inefficient when the first call will return all of the values you need.

Also variable names should be camelCased. It will help tremendously with readability. Maybe check out https://google.github.io/styleguide/jsguide.html if you’re starting out.

I would highly recommend going through this tutorial as well, and actually code it out! https://www.meteor.com/tutorials/blaze/creating-an-app