如何在nestjs中加入两个模式?

时间:2019-10-09 04:57:19

标签: node.js mongoose nestjs

我有两种模式

feature.schema.ts

`import * as mongoose from 'mongoose';
import { settings } from '../_settings';
  export const FeatureSchema = new mongoose.Schema({
     feature_name :{
            type : String
        },
        module_id :{
            type : mongoose.Schema.Types.ObjectId,
            ref : 'Modules'
        },
        status :{
            type : String,
            default: false
        }
}, {...settings.options, collection: 'Features'}
);`

menus.schema.ts

import * as mongoose from 'mongoose';
import { settings } from '../_settings';

export const MenusSchema = new mongoose.Schema({
    name :{
        type : String
    },
    feature_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Features',
    },
    status :{
        type : String
    }
}, {...settings.options, collection: 'Menus'}
);

我尝试加入具有该功能的菜单,尝试像这样加入 menus.service.ts

import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { IDMenus, IDFeature } from '@core/interfaces';
import { MenusDto, FeatureDto } from '@core/dto';
var ObjectId = require('mongoose').Types.ObjectId;

@Injectable()
export class MenusService {
    constructor(@InjectModel('Menus') private dataModel :Model<IDMenus>,
                @InjectModel('Features') private ftModel :Model<IDFeature> ) {} 

  async findjoin(): Promise<IDMenus[]> {
    return this.dataModel.find().populate('Features.feature_id')
    .exec();
  }

}

它的结果没有显示错误,但是为什么没有连接结果呢? 结果是

{
        "_id": "5d6f606bbd12ad52b7ec618f",
        "name": "Inventry",
        "feature_id": "5d6f5d22bd12ad52b7ec618e",
        "status": "TRUE",
        "createdAt": "2019-09-04T06:57:47.212Z",
        "updatedAt": "2019-09-04T06:57:47.212Z",
        "id": "5d6f606bbd12ad52b7ec618f"
    }

如何获得联接结果,这是正确的联接方式?

1 个答案:

答案 0 :(得分:0)

在这样的异步函数中返回等待:return await this.dataModel.find().populate('feature_id').exec()