猫鼬和NestJS-几个问题

时间:2020-09-29 21:31:01

标签: node.js mongodb mongoose nestjs

我想问几个问题,但我会告诉你到目前为止的进展:

这是我的模式

export const langSchema = new mongoose.Schema( {
    heb: String,
    eng: String
})

export interface lang extends mongoose.Document {
  heb: string;
  eng: string;
}

export const catTypeBranSchema = new  mongoose.Schema( {
  name: langSchema,
  changeDate: String,
  changeId: String
})

export interface catTypeBran extends mongoose.Document {
  id: string;
  name: lang;
  changeDate: string;
  changeId: string;
}

export const branchSchema = new mongoose.Schema({
  name: langSchema,
  catTypes: [catTypeBranSchema],
  categories: [categoriesBranSchema],
  payMethods: [payMethodsBranSchema],
  perExpTypes: [perExpTypesBranSchema],
  suppliers: [suppliersBranSchema],
  reports: [reportsBranSchema],
  reminders: [remindersBranSchema],
  vat: [vatBranSchema],
  changeDate: Date,
  changeId: String
})

export interface Branch extends mongoose.Document {
  id: string;
  name: lang;
  catTypes: catTypeBran[];
  categories: categoriesBran[];
  payMethods: payMethodsBran[];
  perExpTypes: perExpTypesBran[];
  suppliers: suppliersBran[];
  reports: reportsBran[];
  reminders: remindersBran[];
  vat: vatBran[];
  changeDate: Date;
  changeId: string;
}

如您所见,我有很多模式,但是它们是相关的。

现在问问题:

  1. 我试图保存一个新的catType,但是因为它具有id属性,所以我不能这样做...

我收到错误消息“ TS2345:类型'{name:lang.lang; changeDate:number; changeId:any;}'的参数不能分配给类型'catTypeBran'的参数。属性'id'在类型中丢失“ {{name:lang.lang; changeDate:number; changeId:any;}”,但在'catTypeBran'类型中为必需。

这是我的尝试:

async findBranch(branchId: string): Promise<Branch> {
    let branch;
    try {
      branch = await this.BranchModel.findById(branchId).exec()
    } catch (e) {
      throw new NotFoundException('Could not find branch ' + branchId)
    }
    if (!branch) {
      throw new NotFoundException('Could not find branch ' + branchId)
    }
    return branch
  }
async addCatType(branchId: string, name: lang.lang, user: any) {
    const returnValue = { statusCode: 200, message: "Success", catTypeId: "" }
    let createResult;
    try {
      const newCatType = {
        name,
        changeDate: Date.now(),
        changeId: user.userId
      }
      const branchToEdit = await this.findBranch(branchId)
      branchToEdit.catTypes.push(newCatType)
      createResult = await branchToEdit.save()
    }
    catch (e) {
      throw new NotFoundException("Error Creating The Cat Type " + name.eng, name.eng)
    }
    if (createResult.id === 0 ) {
      throw new NotFoundException("Error Creating The Cat Type " + name.eng, name.eng)
    }
    returnValue.catTypeId = createResult.id
    return returnValue
  }
  1. 如您所见,我的分支模型中有很多子文档。我想知道是否可以从分支数据中动态选择(甚至动态更改)吗?

  2. 我想知道是否有人可以帮助我仅按subDocument的名称返回subDocument。例如,我要发送“ catTypes”和branchId,它将仅返回给定分支的“ catTypes”数组。

非常感谢, 我很感谢帮手

0 个答案:

没有答案