程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) 如何使用装饰器通过 mongoose / Typescript 在 Nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) 如何使用装饰器通过 mongoose / Typescript 在 nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:?

开发过程中遇到nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) 如何使用装饰器通过 mongoose / Typescript 在 nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:的问题如何解决?下面主要结合日常开发的经验,给出你关于nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) 如何使用装饰器通过 mongoose / Typescript 在 nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:的解决方法建议,希望对你解决nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) 如何使用装饰器通过 mongoose / Typescript 在 nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:有所启发或帮助;

我正在使用 nest.Js 并尝试使用包含子文档字段数组的装饰器创建架构。

导入/导出架构并将其转换为模型,我没有任何问题,直到:

Nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID)
      
    如何使用装饰器通过 mongoose / Typescript 在 Nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:

我在 service 文件中收到以下错误。

经过数小时的谷歌搜索,我发现真正的原因在于 array 子文档字段背后,而且只有它们当我删除 @H_196_14@members 字段时。架构和模型会很好。并且不必对描述的in following或any other answers与extends Mongoose.document相关的解决方案做任何事情。 (如果你已经做过了

我发现的大多数情况与子文档相关,但与数组子文档无关。我想问:

如何使用装饰器通过 mongoose / Typescript 在 nestJs 中正确创建包含子文档数组的字段?

并清除此错误:

S2344: Type 'Guild' does not satisfy the consTraint 'document<any,{}>'.   
  The types returned by 'delete(...).$where(...).cast(...)' are incompatible between these types.
   Type 'updatequery<Guild>' is not assignable to type 'updatequery<document<any,{}>>'.
     Type 'updatequery<Guild>' is not assignable to type '_updatequery<_AllowStringsForIDs<Leandocument<document<any,{}>>>>'.
Types of property '$pull' are incompatible.
  Type 'PullOperator<_AllowStringsForIDs<Leandocument<Guild>>>' has no propertIEs in common with type 'PullOperator<_AllowStringsForIDs<Leandocument<document<any,{}>>>>'.

我的架构是:

import { document,scheR_121_11845@a as MongoosescheR_121_11845@a } from 'mongoose';
import { Prop,scheR_121_11845@a,scheR_121_11845@aFactory } from '@nestJs/mongoose';

class GuildMember {
  @Prop({ type: String,required: true,lowercase: true })
  _ID: String;

  @Prop({ required: true })
  ID: number;

  @Prop({ required: true })
  rank: number;
}

@scheR_121_11845@a({ timestamps: true })
export class Guild extends document {
  @Prop({ type: String,lowercase: true })
  _ID: String;

  @Prop({ type: MongoosescheR_121_11845@a.Types.Array})
  members: GuildMember[]
}

export const GuildsscheR_121_11845@a = scheR_121_11845@aFactory.createForClass(Guild);

做了什么。

各种方式包括:

  • Class 装饰器覆盖子文档 @scheR_121_11845@a() 并添加 extends document
  • type: 添加到字段 @Prop() 装饰器:
  @Prop({ type: [GuildMember] })
  members: GuildMember[]

反之亦然。它是 ok 原语,但不是 Class 嵌入文档。

  • 添加@Prop({ ref: () => GuildMember })

并遵循 nestJs 官方文档:

  @Prop({ type: [{ type: MongoosescheR_121_11845@a.Types.Array,ref: GuildMember }] })
  members: GuildMember[]

它仍然没有帮助。我想,它可能是相关的,不仅与 @H_196_14@mongoose.document 相关,还与另一种类型相关,即:@H_196_14@mongoose.documentArray

更新:

根据目前的进展。问题似乎与默认猫鼬的 fIEld: type 值有关,而不是 @Prop 装饰器本身。所以即使我写了这样的东西:

  @Prop()
  members: Types.Array<GuildMember>

它仍然给出错误。类型导入自:import { document,scheR_121_11845@a as MongoosescheR_121_11845@a,Types } from 'mongoose';

解决方法

@H_673_111@

您的 @H_196_14@members 道具不是一个简单的数组。它是子文档的集合,应该声明为 [scheR_121_11845@aTypes.ObjectId]它将通过默认的 mongo _id 值实现带有 ObjectID 字段的子文档:

@Prop({ type: [scheR_121_11845@aTypes.ObjectId],ref: 'GuildMember'})
members: GuildMember[]
,

我目前的工作解决方案正在使用:

import { Document,scheR_121_11845@a as MongoosescheR_121_11845@a } from "mongoose";
import { Prop,scheR_121_11845@a,scheR_121_11845@aFactory } from '@nestjs/mongoose';

@scheR_121_11845@a()
class Pet extends Document  {
  @Prop()
  _id: number;

  @Prop()
  name: String;
}

export const PetsscheR_121_11845@a = scheR_121_11845@aFactory.createForClass(Pet);

对象字段的父架构数组:

@Prop({ type: [PetsscheR_121_11845@a] })
pets: MongoosescheR_121_11845@a.Types.Array;
@H_843_2@mongoose 模式类型和子文档的单独模式,其中 _id 是我自己预定义的。我不确定,这是这里的最佳做法,但似乎有效。

大佬总结

以上是大佬教程为你收集整理的Nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) 如何使用装饰器通过 mongoose / Typescript 在 Nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:全部内容,希望文章能够帮你解决Nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) 如何使用装饰器通过 mongoose / Typescript 在 Nestjs 中正确创建包含子文档数组的字段?我做了什么。更新:所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。