elasticsearch 使用typescript时未定义Mongoostic搜索函数

nxowjjhe  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(95)

我正在使用mongoose v6.0.0mongoosastic v5.0.0typescript,当我在初始化插件并尝试正确设置类型后尝试以下代码时,它仍然告诉我PageModel.search和插件引入的其他函数是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>>;

// ~ PageModel.search is undefined here

export default PageModel;

我试过将mongoose升级到最新版本,通过documentation升级mongoose类型似乎没有太多信息。

gmxoilav

gmxoilav1#

只是输入错误的PageModel我切换的代码

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

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

相关问题