Home > Software engineering >  How to initialize server-side Meteor Collection without needing to interact with it?
How to initialize server-side Meteor Collection without needing to interact with it?

Time:11-12

I'm new to enter image description here

It seems there's a significant caveat glossed over in the tutorial - this collection needs to not only be imported somewhere in the server/main.ts, but actually interacted with on the server for the plumbing to be initialized.

Every example I've found explains this away by installing some "initial seed data" as a convenience - but I'm having trouble graduating this approach to something more realistic.

If I import the collection in /server/main.ts but don't touch it:

// server/main.ts
import { Meteor } from "meteor/meteor";
import { RecipesCollection } from "/imports/api/recipes";

Meteor.startup(async () => {});

The insert fails with the error in the screenshot above. However, if I do something semi-meaningless like this:

// server/main.ts
import { Meteor } from "meteor/meteor";
import { RecipesCollection } from "/imports/api/recipes";

Meteor.startup(async () => {
  console.log(`On startup, saw ${RecipesCollection.find().count()} recipes`);
});

Then the application functions as expected. Is there a more straightforward way to signal to Meteor that I'd like this collection to be "plumbed" for server-side persistence and interaction with Mongo?

CodePudding user response:

You will always need to import collection code somewhere on the server, but not necessarily interact with it. In anything larger than just a demo this will probably happen naturally because you'll end up importing your collections so that you can write allow/deny logic, attach hooks, or use them within your Meteor methods.

In the case of your above code a simple one line import with just the file path should work.

import "/imports/api/recipies";

CodePudding user response:

I think you are going down a rat hole. The issue is not about interacting with it, I suspect you are simply not sending your inserts from the client to the server. What you are missing is a subscription (on the client) to the collection, or a publication of the collection on the server, or the permission (allow/deny rules) to perform those inserts from the client.

  • Related