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.