【问题标题】:Mongoose 6.0.5 $push typescript errorsMongoose 6.0.5 $push 打字稿错误
【发布时间】:2021-11-05 01:21:56
【问题描述】:

当我更新到最新的 mongoose 版本 6.0.5 时,使用 $push 更新会出现以下错误。查询和更新仍然有效,当我使用 mongoose 6.0.1 时,我没有收到此错误。

No overload matches this call.
  Overload 1 of 3, '(filter: FilterQuery<INotifications>, update: UpdateQuery<INotifications>, options: QueryOptions & { ...; }, callback?: ((err: CallbackError, doc: any, res: any) => void) | undefined): Query<...>', gave the following error.
    Type '{ notificationReplyTravaux: IReplyTravaux; }' is not assignable to type 'undefined'.
  Overload 2 of 3, '(filter: FilterQuery<INotifications>, update: UpdateQuery<INotifications>, options: QueryOptions & { ...; } & ReturnsNewDoc, callback?: ((err: CallbackError, doc: INotifications, res: any) => void) | undefined): Query<...>', gave the following error.
    Type '{ notificationReplyTravaux: IReplyTravaux; }' is not assignable to type 'undefined'.
  Overload 3 of 3, '(filter?: FilterQuery<INotifications> | undefined, update?: UpdateQuery<INotifications> | undefined, options?: QueryOptions | null | undefined, callback?: ((err: CallbackError, doc: INotifications | null, res: any) => void) | undefined): Query<...>', gave the following error.
    Type '{ notificationReplyTravaux: IReplyTravaux; }' is not assignable to type 'undefined'.ts(2769)
index.d.ts(2588, 5): The expected type comes from property '$push' which is declared here on type 'UpdateQuery<INotifications>'
index.d.ts(2588, 5): The expected type comes from property '$push' which is declared here on type 'UpdateQuery<INotifications>'
(property) $push?: undefined

架构

import mongoose, { PopulatedDoc } from 'mongoose';
import { IArchivedOrder } from './ArchivedOrders.model';
import { IReplyTravaux } from './ReplyTravaux.model';

export interface INotifications extends mongoose.Document {
  user: string,
  notificationReplyTravaux: PopulatedDoc<IReplyTravaux & Document>[],
  notificationOrder: PopulatedDoc<IArchivedOrder & Document>[],
}

const NotificationsSchema = new mongoose.Schema<INotifications>({
  user: String,
  notificationReplyTravaux: [
    {
      type: String,
      ref: 'ReplyTravaux',
    },
  ],
  notificationOrder: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'ArchivedOrders',
    },
  ],
});

export default mongoose.model<INotifications>('Notification', NotificationsSchema);

IRplyTravaux 界面

export interface IReplyTravaux extends mongoose.Document {
  _id: string,
  message: string,
  author: {
    id: string,
    name: string,
  },
  travaux: string | ITravaux,
  createdAt: Date,
}

【问题讨论】:

    标签: node.js typescript mongodb mongoose


    【解决方案1】:

    我遇到了类似的问题。为了解决我的问题,我必须删除 @types/mongoose 作为依赖项,从 5.11.0 开始,mongoose 包有自己的类型。

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 2018-06-05
      • 2021-08-11
      • 1970-01-01
      • 2019-01-24
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多