类型被推断为 any with find moongose typescript

安东尼·卢兹奎尼奥斯

我的models/areas.ts文件中有以下内容

import { Document, model, Schema } from 'mongoose'
import uniqueValidator from 'mongoose-unique-validator'

interface IAreas extends Document {
  abstract: string,
  code    : number,
  image   : string,
  name    : string
}

const Areas = new Schema(
  {
    abstract: {
      required: true,
      type    : String
    },
    code: {
      default: 1,
      type   : Number
    },
    image: {
      default: '',
      type   : String
    },
    name: {
      required: true,
      type    : String,
      unique  : true
    }
  }
)

Areas.plugin(uniqueValidator)

const AreasModel = model<IAreas>('areas', Areas)

export { AreasModel, IAreas }

我的controller/areas.ts文件中有以下内容

import { AreasModel, IAreas } from '../models/areas'

const getAll = async (): Promise<IAreas[]> => {
  try {
    const areas = await AreasModel.find({}, '-__v')

    return areas
  } catch (error) {
    console.log('There was a problem trying to get all the areas.')
    throw error
  }
}

所以,areas我遇到的问题是变量被推断为任何变量,我不知道为什么,我有其他项目可以正确推断它。

证据

这是我正在使用的版本:

typescript: ^4.1.3
ts-node: ^9.1.1
mongoose: ^5.11.12
@types/mongoose: ^5.10.3
安东尼·卢兹奎尼奥斯

好吧,解决我的问题的是删除@types/mongoose@types/mongoose-unique-validator包。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章