Getting data from array or object in for loop and async/await

I have been working to migrate an app from meteor 2.x to meteor 3. Needless to say I think I understand async/await now. However, in a method I have I am trying to insert data to a collection using loops, because I collect multiple values in a couple of fields that need to be added to their own document in the collection.

e.g. tasks = “Take out Trash”, “Walk the dog”, “Run the Dishwasher”, dates = 2025-06-25, 2025-06-28, 2025-07-02

For each date I need to add a task to complete. So with this setup, I’ll have 9 separate documents.

I was trying to move to the

for (task in taskName) {
    for (date in taskDate) {

    }
}

style looping, but I don’t get the values when I do this, but an index number.

I could just go back to for (let i = 0; i < taskName.length; i++) style, but would prefer to learn something new, and make it more readable as well.

Also, when I do use the i / j looping, the console only showed each set of date with task once, but they were inserted intot he db multiple times with the same date and task.

Here’s my full method:

'add.myTask' (taskName, taskDate, actDate) {
        check(taskName, [Object]);
        check(taskDate, [String]);
        check(actDate, [Date]);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.');
        }

        let i;
        let j;
        let tnLen = taskName.length;
        let tdLen = taskDate.length;

        const uInfo = async() => {
            let userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
            if (!userInfo) {
                console.log("No matching user info found.")
            } else {
                for (const date in taskDate) {
                    for (const task in taskName) {  
                        try { 
                            await TaskItems.insertAsync({
                                taskName: task,
                                taskDate: date,
                                actualDate: "hold",
                                assignedTo: userInfo.username,
                                assignedToId: this.userId,
                                isComplete: false,
                                completedOn: null,
                                assignedOn: new Date(),
                                assignedBy: this.userId,
                            });
                        } catch(error) {
                            console.log("    ERROR adding tasksL " + error.message);
                        }
                    }
                }
                return true;
            }
        }
        uInfo(); 
    }

I’m ignoring the actDate array for now, and will deal with it after I get this stuff working.

Any help is appreciated as always.

In async/await you need to use an async loop or risk having further code run before a loop is completed. In a simple for loop, syncronuos code should not be a problem but if you have heavy calculations or fetch data you will need to async it and await on it (if you need/want to)

This doesn’t say anything about your data types but if you get indexes you probably have an array.


// Example async function that simulates a delay
async function fetchData(item) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(`Processed ${item}`);
    }, 1000);
  });
}

async function processArray() {
  const items = ['apple', 'banana', 'cherry'];

  for (const item of items) {
    const result = await fetchData(item);
    console.log(result);
  }
}

processArray();
// if you await processArray() the next code after it will wait for it to complete. If you don't await it, the next code will proceed while the processArray() it does its job.


ok, the taskName variable is an array of objects.

[ { id: "Take out trash"}, { id: "Run dishwasher"}, ... ]

The taskDate variable is an array of dates.

so you’d need to go for JavaScript For Of

1 Like