【问题标题】:mongoose Model is not callable tslint error猫鼬模型不可调用 tslint 错误
【发布时间】:2020-08-03 14:42:58
【问题描述】:

我有一个简单的 Nest js 服务,如下所示:

import { Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { Collection } from './interfaces/collection.interface';
import { CollectionDto } from './dto/collection.dto';
import { COLLECTION } from '../constants';

@Injectable()
export class CollectionsService {
  constructor(
    @InjectModel(COLLECTION) private readonly collectionModel: Model<Collection>
  ) {}

  async getAllCollections(): Promise<Collection[]> {
    const collections = await this.collectionModel.find().exec();
    return collections;
  }

  async addCollection(collectionDto: CollectionDto): Promise<Collection> {
    const newCollection = await this.collectionModel(collectionDto);
    return newCollection.save();
  }
}

代码运行良好,但我收到 tslint 警告 ts(2348)。有人知道如何以不同于使用// @ts-ignore 规则的方式解决它吗?

【问题讨论】:

  • 将光标悬停在错误上时按 alt+F8 应该会给出原因
  • 它给了我同样的信息Value of type 'Model&lt;Collection, {}&gt;' is not callable. Did you mean to include 'new'?ts(2348)

标签: mongodb typescript mongoose nestjs tslint


【解决方案1】:

使用new 对我有用。我就是这样做的。试试看。

const newCollection = await new this.collectionModel(collectionDto);

【讨论】:

    【解决方案2】:

    尝试返回new newCollection.save(); 架构是构造函数。

    这适用于 Node.js,因此如果我们都使用 Mongoose,它也应该适用于 React。

    【讨论】:

    • 这不起作用,为什么会起作用?在我看来,这是 Mongoose @types npm 库或 ts lint 的问题。
    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2022-07-05
    • 1970-01-01
    • 2015-02-12
    • 2021-08-19
    • 1970-01-01
    相关资源
    最近更新 更多