I have a front end Blaze page with Meteor 3, and a meteor method call to insert data into a collection that no matter what I do I end up with duplicate entries in the database.
I need some sanity checks for sure, but I think I’m doing everything as far as the looping properly here.
Front End:
HTML Template
<template name="taskForm">
<h4>Tasks</h4>
<form class="row" style="gap: 1em;">
<div class="col s12 m6 l4 chips chips-placeholder" id="taskName">
</div>
<div class="col s12 m6 l4 input-field">
<select name="taskUser" id="taskUser" class="taskUser">
<option value="" disabled selected>Assign to user...</option>
{{#each taskUsers}}
<option value="{{username}}_{{usersId}}">{{username}}</option>
{{/each}}
</select>
</div>
<div class="col s12 m6 l4 input-field">
<input type="text" class="datepicker" id="taskDate" />
<label for="taskDate">Task Date (multiple entries)</label>
<div class="row">
{{#each taskDate in taskDates}}
<div class="col s6 l3 m4">
<span>{{taskDate}}</span>
</div>
{{/each}}
</div>
</div>
<div class="col s12 m6 l6">
<p>
<label>
<input type="checkbox" id="hideCompletedTasks" />
<span>Hide Completed</span>
</label>
</p>
</div>
<div class="col s12 m6 l6">
<a class="waves-effect waves-light btn saveTaskMgmt green white-text right">Add</a>
<!-- <a class="waves-effect waves-light btn testChips blue white-text left" id="testChips">Test Chips</a> -->
</div>
</form>
{{> snackbar}}
</template>
Javascript for page:
'click .saveTaskMgmt' (event) {
event.preventDefault();
let elemcc = document.getElementById('taskName');
let taskNameArr = M.Chips.getInstance(elemcc).chipsData;
let taskDateArr = Session.get("taskDateArr");
let taskUser = $("#taskUser").val();
let taskUserErr = false;
let taskNameErr = false;
let taskDateErr = false;
let userInfo;
let actDate = [];
if (taskNameArr == null || taskNameArr == []) {
taskNameErr = true;
}
if (taskDateArr == null || taskDateArr == []) {
taskDateErr = true;
} else {
for (i = 0; i < taskDateArr.length; i++) {
// console.log(taskDateArr[i]);
let actDateTask = new Date(taskDateArr[i]);
actDate.push(actDateTask);
}
}
if (taskUser == null || taskUser == "") {;
taskUserErr = true;
} else {
userInfo = taskUser.split('_');
}
if (taskUserErr == false && taskDateErr == false && taskNameErr == false) {
const addTask = async() => {
let result = await Meteor.callAsync("add.task", taskNameArr, userInfo[0], userInfo[1], taskDateArr, actDate);
if (!result) {
console.log(" ERROR adding the new task.");
} else {
console.log(" SUCCESS adding the new task.");
Session.set("taskDateArr", []);
$("#taskDate").val("");
$("#taskUser").val("");
$('select').formSelect();
}
}
addTask();
} else {
showSnackbar("ERROR: Missing Required Fields!", "red");
}
Back End:
Method Call:
'add.myTask' (taskName, taskDate, actDate) {
check(taskName, String);
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.');
}
const uInfo = async() => {
let userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
if (!userInfo) {
console.log("No matching user info found.")
} else {
try {
await TaskItems.insertAsync({
taskName: taskName,
taskDate: taskDate,
actualDate: actDate,
assignedTo: userInfo.profile.fullname,
assignedToId: this.userId,
isComplete: false,
completedOn: null,
assignedOn: new Date(),
assignedBy: this.userId,
});
} catch(error) {
console.log(" ERROR adding tasks " + error.message);
}
}
}
uInfo();
If I use this to enter 2 tasks, ‘trash’ and ‘mow lawn’, on 2 dates, then I see this in the log output:
I20250721-16:38:17.961(-5)? 'trash'
I20250721-16:38:17.973(-5)? 'Jul 22, 2025'
I20250721-16:38:17.973(-5)? 2025-07-22T05:00:00.000Z
I20250721-16:38:17.973(-5)? ---------------------------------
I20250721-16:38:17.974(-5)? 'trash'
I20250721-16:38:17.974(-5)? 'Jul 23, 2025'
I20250721-16:38:17.974(-5)? 2025-07-23T05:00:00.000Z
I20250721-16:38:17.974(-5)? ---------------------------------
I20250721-16:38:17.975(-5)? 'mow lawn'
I20250721-16:38:17.975(-5)? 'Jul 22, 2025'
I20250721-16:38:17.975(-5)? 2025-07-22T05:00:00.000Z
I20250721-16:38:17.975(-5)? ---------------------------------
I20250721-16:38:17.977(-5)? 'mow lawn'
I20250721-16:38:17.977(-5)? 'Jul 23, 2025'
I20250721-16:38:17.977(-5)? 2025-07-23T05:00:00.000Z
I20250721-16:38:17.977(-5)? ---------------------------------
but I end up with this from mongoshellL
meteor [direct: primary] meteor> db.taskitems.find({}).count();
8
I’m sure I’ve got something odd happening, here, but I have not idea what it is. Any help is greatly appreciated.