Utilizing Mongoose RefPath in NestJS to execute populate() operation

After registering my Schema with mongoose using Dynamic ref, I followed the documentation available at: https://mongoosejs.com/docs/populate.html#dynamic-ref

@Schema({ collection: 'quotations' })
export class QuotationEntity {
  
  @Prop({ 
    required: true,
    enum: {
      values: ['PersonalClientEntity', 'CommercialClientEntity'],
      message: 'Please supply a valid client type. Allowed: \'PersonalClientEntity\' or \'CommercialClientEntity\'.'
    },
    type: String
  })
  clientType: String;

  @Prop({ type: MongooseSchema.Types.ObjectId, refPath: 'ClientType', required: true })
  clientRef: Types.ObjectId;
}

I save an ObjectId under the clientRef field which should reference the clientType field. When I use the populate() method, it needs to populate either 'PersonalClientEntity' or 'CommercialClientEntity'.

When running the query:

await this._model.find({ companyRef: companyId }).populate('clientRef').exec();

No population occurs. However, replacing the ref in my Schema and providing the correct reference manually like so:

@Prop({ type: MongooseSchema.Types.ObjectId, ref: 'PersonalClientEntity', required: true })
  clientRef: Types.ObjectId;

The populate() method works as expected. Am I missing something in my Schema with the refPath, or is there something else I am overlooking?

Answer №1

Upon returning to the problem after taking a break, I quickly realized my mistake. It turns out that I made a silly spelling error like a true numskull.

Here is my refPath:

refPath: 'ClientType'

And here is my model:

clientType: String;

Can you spot the issue? Can you spot it? Yes, I definitely felt like kicking myself for this oversight.

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

Checkbox: Customize the appearance of the root element

I am trying to modify the root styles of a Checkbox component, but it is not working as expected. Here is my code: <CheckboxItem onChange={()} checked={isChecked} label="Show Checkb ...

Encountering Error 203 while trying to establish a connection between Angular and Django Rest Api

I'm currently working on a project that involves creating a contacts system, but I've been encountering errors when trying to list them. Interestingly, I can successfully perform CRUD operations using Postman with the API. One of the messages I ...

Using a component again with variations in the input data

I'm looking to reuse a card component to display different types of data, but the @Input() properties are for different objects (articles and events). Parent HTML: <card-component [headline]="Articles" [content]="art ...

Transmit information using the buttonRenderer feature in Ag-Grid for Angular applications

Struggling to transfer data between two unrelated components by clicking on a cell in ag-Grid and passing the data to a form component. I utilized the buttonRenderer function to extract row data from ag-Grid, but I'm unsure how to pass it to the secon ...

The object assigned in the Facebook login method cannot be utilized

I'm working on integrating Facebook login with Angular 2. Here's the button for logging in: <button ion-button (click)="fbLogin()"><ion-icon name="logo-facebook"></ion-icon>Login using Facebook</button> Below is the clic ...

Tips for preventing repetition in http subscribe blocks across various components

Imagine a scenario where there is a service used for making HTTP request calls. There are two different components (which could be more than two) that need to send the same request using the same observables via this service. After receiving the result, it ...

Facing issues with the Node.js mongoose module's functionality when deployed on Heroku

Recently, our website started encountering an Application Error: "An issue occurred within the application preventing your page from loading. Please try again shortly. If you are the owner of the application, review your logs for more information." Upo ...

What is the proper way to indicate that a function parameter corresponds to one of an Interface's keys?

When working with TypeScript, I am looking for a way to validate that the argument passed to myFunction matches one of the keys defined in MyInterface. Essentially, I want to enforce type checking on the arg parameter as shown below. export interface MyInt ...

The constant value being brought in from an internal npm package cannot be determined

I have developed an internal npm package containing shared types and constants. My project is built using TypeScript with "target": "ESNext" and "module": "NodeNext". Within one of my files, I define: export type Su ...

Show a dropdown menu based on a certain condition in Angular

Is there a way to conditionally display select options like this? <select id="updateType" class="form-control" formControlName="updateType"> <option value="personalDetails">Personal</option> <option value="addressD ...

Is your async function lacking a return statement?

After completing the development of a new method for a bug I encountered, I noticed something interesting. Despite the fact that there is a potential scenario where the function may not return anything, the compiler did not flag any errors. It got me think ...

Encountering challenges with Object-Oriented Programming combined with Typescript: Are you experiencing a

Currently, I'm in the process of building a comprehensive authentication application using the MERN stack entirely in TypeScript. However, I am encountering some issues (specifically type errors) with my userController file. Here is my routes file: i ...

Differences in weekend start and end days vary across cultures

Looking for a solution to determine the weekend days per culture code in Typescript/Javascript? While most countries have weekends on Sat-Sun, there are exceptions like Mexico (only Sunday) and some middle-eastern countries (Fri-Sat). It would be helpful ...

A guide on how to check for the existence of a value within an array subdocument in mongoose and return a Boolean

If my user schema is structured as shown below: const user = new schema( Id: { type: ObjectId, }, followers: { type: Array, default: [], }, ) I need to create a query that will iterate through the followers array and return a boolean v ...

"Update your Chart.js to version 3.7.1 to eliminate the vertical scale displaying values on the left

Is there a way to disable the scale with additional marks from 0 to 45000 as shown in the screenshot? I've attempted various solutions, including updating chartjs to the latest version, but I'm specifically interested in addressing this issue in ...

The function is attempting to access the 'lockDatabase' property of an undefined object, resulting in an error

I'm encountering an error due to the scope issue with 'this', and I'm struggling to find a solution. I attempted using the fat arrow, which solved the scope problem but created another issue where I lack a callback value that needs to b ...

Creating secure private messaging functionality with Socket.io, Express.js, Node.js and Mongoose

I'm currently facing difficulties with implementing socket.io to create a chat application that connects users who are logged into my platform. Although I successfully developed a private chat app following a beginner course, the users involved in the ...

Looking for assistance in setting up a straightforward TypeScript Preact application

I recently started exploring preact and I'm attempting to create a basic app using typescript in preact. I've noticed that their default and typescript templates include extras like jest and testing, which I don't necessarily require. Althou ...

Ways to parse the data from a response received from an Axios POST request

After sending the same POST request using a cURL command, the response I receive is: {"allowed":[],"error":null} However, when I incorporate the POST request in my code and print it using either console.log("response: ", resp ...

Detecting Typescript linting issues in VSCode using Yarn version 3.2.3

Every time I try to set up a new react or next app using the latest yarn v3.2.3, my VS Code keeps showing linting errors as seen in the screenshot below. The main error it displays is ts(2307), which says Cannot find module 'next' or its correspo ...