Error encountered: Mongoosastic search doesn't work properly in TypeScript environment

Currently, my setup involves using mongoose v6.0.0, mongoosastic v5.0.0, and integrating typescript. However, even after initializing the plugin and attempting to correctly set the types, I am encountering issues where functions like PageModel.search introduced by the plugin are showing up as undefined.

import mongoose, { Schema, Document } from 'mongoose';
import mongoosastic, { MongoosasticModel, MongoosasticDocument } from 'mongoosastic'

export interface IPage extends Document, MongoosasticDocument {
  user: string;
  permissions: {
    [key: string]: {
      read: boolean;
      write: boolean;
      admin: boolean;
      email: string;
    };
  };
  style: {};
  data: {
    blockType: string;
    properties: {};
    children: [];
  }[];
}

const PageSchema = new Schema({
  user: String,
  permissions: {},
  style: {},
  data: [
    {
      blockType: String,
      properties: {},
      children: [],
    },
  ],
});

// -=- Elastic Search -=-
// ~ Check if Elastic Search info exists
if (!process.env.ELASTICSEARCH_URL) throw Error('Missing Elastic Search URL');

// ~ Add Elastic Search plugin
PageSchema.plugin(
  mongoosastic,
  {
    clientOptions: {
      nodes: [
        process.env.ELASTICSEARCH_URL,
      ]
    },
    transform: (doc: IPage) => {
      
    },
  }
);

const PageModel = (mongoose.models.page || mongoose.model('page', PageSchema)) as mongoose.Model<IPage, MongoosasticModel<IPage>>;

// ~ The issue arises with PageModel.search being undefined at this point

export default PageModel;

I have attempted upgrading mongoose to its latest version and also updating the mongoose types following the guidance provided in the documentation. However, none of these actions have resolved the issue.

Answer №1

Accidentally mistyped the PageModel and made a switch in the code

const PageModel = (mongoose.models.page || mongoose.model('page', PageSchema)) as mongoose.Model<IPage, MongoosasticModel<IPage>>;

changed to

const PageModel = (mongoose.models.page || mongoose.model('page', PageSchema)) as MongoosasticModel<IPage>;

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

Tips for correctly displaying diacritics with Webpack and Typescript

While working on my project, I encountered an issue with diacritics marks (such as German or Polish characters) when using Webpack with Typescript. Unfortunately, the problem arises when trying to display these marks in the console or on a webpage. It seem ...

Displaying elements in a multi-dimensional array using PHP

After exploring numerous similar questions, I have yet to find a solution to my specific issue. In my database, I have a PHP nested array that needs to be printed in its entirety. The current output only displays the values of the topmost array ('name ...

Issue connecting database with error when combining TypeORM with Next.js

I am attempting to use TypeORM with the next.js framework. Here is my connection setup: const create = () => { // @ts-ignore return createConnection({ ...config }); }; export const getDatabaseConnection = async () => { conso ...

Exploring the capabilities of indexing an array within an array in Nativescript

I am working with JSON data retrieved from an API and trying to figure out how to index an array inside an array using Nativescript. JS: cart [{ JS: "_id": "5d3988cd287bad2943686920", JS: "userid": "11E76234942299FCC13FFA163EDC2079", JS: "products": ...

Creating a unique Angular 2 Custom Pipe tutorial

I've come across various instances of NG2 pipes online and decided to create one myself recently: @Pipe({name: 'planDatePipe'}) export class PlanDatePipe implements PipeTransform { transform(value: string): string { return sessionStor ...

Steps for utilizing Bazel to compile TypeScript

Calling all Bazel (Blaze) experts: I'm curious about the best method for integrating Bazel as a build system for cutting-edge web applications built in Typescript. Is there a preferred setup or perhaps a template that demonstrates this integration? T ...

Jhipster 4 project performs well initially, however, upon creating an entity, the page appears empty despite the application running

I am currently working on a project and trying to run it following the steps outlined in https://github.com/JinnaBalu/jhipster4-mongodb. The project runs smoothly, however, after generating an entity using "yo jhipster:entity", I tried to run it again bu ...

Tips for incorporating parameters from the component.ts into @angular/animations

Here is an example of how you can incorporate Angular animations with parameters directly from the HTML file: **Animations.ts** trigger('slowXMove', [ state('posX1State', style({ left: '{{posX1}}px' }), {params: {posX1: ...

Tricks to access value from a Nativescript array of Switch elements when tapping a Button

Scenario: In my project, I am using Nativescript 5.0 with Angular. The data is fetched from an API and displayed in customers.component.ts I am successfully rendering the elements based on the received IDs in customers.component.html When the user inter ...

Accented string values fetched from MongoDB and imported into LiveCode

It's been some time since my last post on StackOverflow. I have a query for you. I am facing an issue where the text "Se cambiarán los bornes" (with accent) appears correctly in a LiveCode field, gets saved properly to a Mongo collection and is vis ...

Check if any elements in the input array match any keywords in the schema attribute array in Mongoose / MongoDB

Currently seeking assistance in determining the most efficient method for creating a query to search an array of keywords. I am hoping to conduct a search using the array ["work", "hi"] compared to an array within the schema: ["work", "school"] and retri ...

How to fetch a single element from an array in MongoDB

If I have a document like this: { "id":"1", "references":["AD1","AD2","AD3"] } I want to extract the single value ("AD1") from the array. Is there a way to do that with a MongoDB query? I've tried several methods, but they only return the entire arr ...

Monitoring mouse events on a child element with an attached directive in Angular 7

Is it possible to capture mouse events that occur only within the child element of the directive's host element using @HostListener()? <div listnerDirective class="parent"> <div class="child> <------ Listen for mouse events here ...

Reducing SCSS import path in Angular 7

Creating a component that is deeply nested raises the issue of importing shared .scss files with long paths: @import '../../../app.shared.scss'; This hassle doesn't exist when it comes to .ts files, thanks to the configuration in tsconfig. ...

Tips for selecting specific regions on an Angular SVG map

For my Angular TypeScript project, I included a map. Does anyone know how to create a select region on the map? Click here for StackBlitz Here is the jsFiddle code link CSS styles here p { font-size: 12px; } #core { fill: #ff4f81; animatio ...

When trying to integrate Angular.ts with Electron, an error message occurs: "SyntaxError: Cannot use import statement

Upon installing Electron on a new Angular app, I encountered an error when running electron. The app is written in TypeScript. The error message displayed was: import { enableProdMode } from '@angular/core'; ^^^^^^ SyntaxError: Cannot use impor ...

Creating an array of Form Groups involves first initializing an empty array and then

Here is a JSON object that I need to create a reactive form based on. What steps should I take for the array portion specifically? { "examName" : "java", "password" : "1234.com", "examCategory" : { "id" : 78 }, "examDetailSet" ...

mat-tab-group - Positions elements in the center, right, and left for optimal alignment

Is it possible to align the buttons in a mat-tab-group to the left, center, and right positions? I am using mat-tabs. How can I have elements with "left" align to the left, elements with "center" in the center, and elements with "right" align to the right? ...

Nodemailer attachments are missing contents

I'm having trouble attaching files to emails using NodeMailer. When I check the sent email, the attachments appear as empty files with 0 bytes. Even when I download them, they are still empty text files. Can someone please help me identify what I migh ...

Transform Dictionary JSON into Typescript model

How can I convert the provided JSON structure into a TypeScript interface to ensure type safety? { "attributes1": { "myKey1": { "myinnerKey11": { "value": "value 1", "name&q ...