What is the process for setting up a server-side Meteor Collection without the necessity of direct interaction?

I'm just starting out with Meteor and working on a basic recipe list.

In my file (/imports/api/recipes.ts), I have defined my Recipe collection:

// imports/api/recipes.ts
export interface Recipe {
  _id?: string;
  title: string;
  createdAt: Date;
}

export const RecipesCollection = new Mongo.Collection<typeof Recipe>("recipes");

It's easy to import and work with this collection on the client side (in a React component).

But here's the issue: Any changes I make don't seem to stick! Inserts briefly appear, but then revert in the browser when the "Optimistic UI" realizes the operation failed:

https://i.sstatic.net/qjuZb.png

The only way I've found to make it work is by interacting with the imported collection in server/main.ts. Simply including it via a named import doesn't do the trick.

Most resources suggest installing some "initial seed data" as a workaround, but I'm struggling to apply this concept in a more practical scenario.

If I import the collection in /server/main.ts without any action:

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

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

The insert fails with the error shown in the screenshot above. However, if I perform a trivial action 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 everything works as expected. Is there a simpler way to indicate to Meteor that I want this collection to be setup for server-side persistence and interaction with Mongo?

Answer №1

When working with collections in Meteor, it is essential to import the collection code on the server, even if you may not directly interact with it. This becomes especially important in larger projects where you might need to write allow/deny logic, attach hooks, or use the collections in your Meteor methods.

In the example provided, a simple one-line import statement with just the file path should suffice:

import "/imports/api/recipies";

It's worth noting that the reason for this requirement is due to the typescript compiler removing imports where all references are unused.

https://github.com/microsoft/TypeScript/issues/4717

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing a GraphQL application with NestJS integrating the Passport LinkedIn strategy

Currently, my nestjs application is up and running on typescript, Graphql, Postgres with Jwt strategy all set. Now, I am looking to integrate the LinkedIn strategy into it. However, I'm unsure about where to begin as most available packages like do no ...

How to update an Array<Object> State in ReactJS without causing mutation

In my program, I store an array of objects containing meta information. This is the format for each object. this.state.slotData [{ availability: boolean, id: number, car: { RegistrationNumber : string, ...

If an interface property is set as (), what significance does it hold?

While exploring the Vue.js source code located at packages/reactivity/src/effects.ts, I came across this snippet: export interface ReactiveEffectRunner<T = any> { (): T effect: ReactiveEffect } I'm curious, what does () signify in the code ...

Exploring the differences between arrays and filter functions within MongoDB aggregation operations

Recently, I crafted a sophisticated pipeline for my database: let orders = await Order.aggregate( { $unwind: "$candidate", }, { $lookup: { from: "groups", localField: & ...

Utilizing Ramda lenses for composition in Typescript with the useState set function in React

I am currently learning functional programming and exploring event handling in React. Let's consider the following example: interface Todo { task: string done: boolean } interface TodoProps { todo: Todo onChange: ChangeEventHandler< ...

Retrieving data from Typescript enums using strings

I need assistance extracting values from this enum: enum Sizes { Tiny = "Tiny", VerySmall = "Very Small", Small = "Small", Medium = "Medium", Large = "Large", VeryLarge = "Very Large" } Although I followed recommendations from other sources, ...

Next.JS-13 has encountered an inappropriate middleware in its App Based Routing functionality

I am currently working on developing a user authentication system that includes features for Login, Signup, and Logout. As part of this project, I have created a middleware.ts file in the src directory, which is located next to the app directory. Below is ...

Creating objects based on interfaces

After looking at this straightforward code: interface int1 { aa: string, bb: number, } const obj1:int1 = {} //#1 function fun(param_obj:int1) { //#2 } I am curious as to why the compiler throws an error: Type '{}' is missing the fol ...

Deploying AWS CDK in a CodePipeline and CodeBuild workflow

I am currently attempting to deploy an AWS CDK application on AWS CodePipeline using CodeBuild actions. While the build and deploy processes run smoothly locally (as expected), encountering an issue when running on CodeBuild where the cdk command fails w ...

The assigned type does not match the type 'IntrinsicAttributes & { children?: ReactNode; }'. This property is not assignable

I have been struggling to resolve this issue, but unfortunately, I have not found a successful solution yet. The error message I am encountering is: Type '{ mailData: mailSendProps; }' is causing an issue as it is not compatible with type &apos ...

Can you explain the significance of the syntax provided?

I've been going through the Angular tutorial, but I'm having trouble grasping the significance of this particular code snippet: return (error: any): Observable<T> => {...}; It seems like the function is returning another function, but ...

Ways to extract a return from an Observable

Do you know how to retrieve the _value from the following code snippet: Here is the function I am referring to: jobsLength(){ const jobslength:any; jobslength=this.searchLogic.items$ console.log(jobslength) }; ...

Describing the implementation of UNO out parameters within definitions

The UNO API offers support for inout and out parameters. In this scenario, a variable is passed into a function, the function modifies the variable, and the updated value of the variable can be accessed outside the function. Javascript lacks native suppor ...

I'm having trouble displaying the saved percentage value in an angular material mat select component

When I use the mat-form-field and mat-option to list numbers 1-100, the saved value is not displaying in the dropdown after saving. <mat-form-field class="full-wid" appearance="outline"> <mat-label>Percentage 1 (%)</mat-la ...

What causes an inference site to have varying effects when accessed directly versus when it is retrieved from a function?

Below is the provided code snippet : declare class BaseClass<TValue = any> { value: TValue; foo(value: TValue): void; } type Wrapped<T> = { value: T } declare class ConcreteClasss<TValue> extends BaseClass<TValue> { construc ...

Can you please provide information on the callback function type used in the filter method of TypeScript?

transactionsData = [ { id: 101, name: 'transaction 1' }, { id: 201, name: 'transaction 2' }, { id: 301, name: 'transaction 3' }, { id: 401, name: 'transaction 4' } ]; constructor( private objGreetingsSe ...

What steps should I take to troubleshoot this Angular issue within a Visual Studio 2022 project that utilizes the Standalone Angular template?

After going through this tutorial and meticulously following each step, I encountered an error when trying to run the application: https://i.sstatic.net/EvYgg.jpg Can anyone advise on how to resolve this issue? I'm facing a similar error while attem ...

Is Mongodb making an asynchronous request?

How can I ensure synchronous execution of code in this scenario? Despite trying various methods, the issue persists where res.render is called too early and certain objects from state are missing. The playingCollection refers to a MongoDB collection. va ...

What is the syntax for declaring a variable with multiple types in Angular/Typescript?

Imagine having a variable declared with the following type: test?: boolean | { [key in TestEnum ]: boolean }; Now, let's assign this test variable within a constant where it can hold either a boolean value or a mapping to an enum. Consider the exampl ...

What is the safest method to convert a nested data structure into an immutable one while ensuring type safety when utilizing Immutable library?

When it comes to dealing with immutable data structures, Immutable provides the handy fromJs function. However, I've been facing issues trying to integrate it smoothly with Typescript. Here's what I've got: type SubData = { field1: strin ...