Tasks Not Saving to Database in Meteor.js 3 + React To-Do App – Subscription or Insertion Issue?

I’m currently building a To-Do app using Meteor.js 3.0 and React, and I’m facing an issue where my tasks aren’t getting saved to the database when added via the form. The data is being passed correctly, but the tasks aren’t appearing in the UI after submission.

I’m using the Meteor.callAsync("tasks.insert", { text: text.trim(), createdAt: new Date() }) method to insert tasks into the collection, and I’ve ensured that MongoDB is properly configured. But after submission, the tasks are not showing in the list, and I’m not receiving any error messages.

Has anyone encountered this issue before? Could this be a subscription issue, or am I missing something in how the tasks are inserted?

Here’s the link to the Meteor.js + React documentation I’m following: (docs.meteor.com/tutorials/react/)

Here’s my code for the task insertion:

import React, { useState } from "react";
import { TasksCollection } from "/imports/api/TasksCollection";

export const TaskForm = () => {
  const [text, setText] = useState("");

  const handleSubmit = async (e) => {
    e.preventDefault();
    if (!text) return;
    try {
      await Meteor.callAsync("tasks.insert", {
        text: text.trim(),
        createdAt: new Date(),
      });
      setText(""); // Reset the input field
    } catch (error) {
      console.error("Error inserting task:", error);
    }
  };

  return (
    <form className="task-form" onSubmit={handleSubmit}>
      <input
        type="text"
        placeholder="Type to add new tasks"
        value={text}
        onChange={(e) => setText(e.target.value)}
      />
      <button type="submit">Add Task</button>
    </form>
  );
};

And my publication:

Meteor.publish("tasks", function () {
  const userId = this.userId;
  if (!userId) return this.ready();
  return TasksCollection.find({ userId });
});

Any advice or solutions would be greatly appreciated!

Step 1: be certain what the problem is?

Have you checked the DB if the doc is inserted ot not?

1 Like

Hi @loufield,

you can use MongoDB Compass to visualize your data, your DB would be running on port 3001 in development if your Node (Meteor) is running on the default port 3000.

It would be interesting to see your method and your subscription as well.

Thank you so much for the suggestion.

You will need to define tasks.insert method on the server.

Did you import your methods file on the server and on the client (if you want optimistic UI)?

import "../imports/api/tasksMethods";