打字稿猫鼬和子文档-创建不存在

时间:2018-12-20 09:46:31

标签: mongodb typescript mongoose

我有一个称为实例的模型和一个称为请求的子文档。我想创建一个新的请求子文档并将其插入instance.requests数组。

   const instance = await Instance.findById(params.id);

   ..

   const instanceRequest = instance.requests.create({
       action: "document",
       type: input.side,
   });

但是在这里我遇到以下错误

  

[ts]类型的属性'create'不存在   “ IInstanceRequestDocument []”。 [2339]

instance.model.ts

import { Schema, Model, Document, model } from "mongoose";
import { IInstanceDocument } from "./instance.interface";
import { RequestSchema } from "./request/request.model";

const InstanceSchema = new Schema({

    jobId: {
        type: String,
        required: false,
        unique: true,
    },
    productId: {
        type: String,
        required: true,
    },
    status: {
        type: String,
        default: "Pending",
    },
    result: {
        type: Schema.Types.Mixed,
    },
    requests: [RequestSchema],

    callback_at: {
        type: Date,
    },

}, {
        timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
    });

// implementation of custom methods here

export const Instance: Model<IInstanceDocument> =
    model<IInstanceDocument>("Instance", InstanceSchema);

instance.interface.ts

import { Document, Model } from "mongoose";
import { IInstanceRequestDocument } from "./request/request.interface";

export interface IInstance {

    _id?: string;
    productId?: string;
    status?: string;
    results?: any;
    requests: IInstanceRequestDocument[];

}

export type IInstanceDocument = IInstance & Document;

request.model.ts

import { Schema } from "mongoose";

export const RequestSchema = new Schema({

    status: {
        type: String,
        default: "Pending",
    },
    action: {
        type: String,
        require:  true,
    },
    type: {
        type: String,
    },

}, {
        timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
});

request.interface.ts

import { Document, Model } from "mongoose";

export interface IInstanceRequestInterface {

    _id?: string;
    status?: string;
    transaction?: any;
    action?: string;
    type?: string;

}

export type IInstanceRequestDocument = IInstanceRequestInterface & Document;

instance.requests属性仅向我返回数组方法列表。

0 个答案:

没有答案