最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 4浏览 0评论

如何在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"
    }

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

我有两种模式,例如feature.schema.ts`import * as from'mongoose';从“ ../_settings”导入{设置};导出const FeatureSchema = new mongoose.Schema({feature_name:{...

回答如下:

.populate('Features.feature_id')替换.populate('feature_id')

如何在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"
    }

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

我有两种模式,例如feature.schema.ts`import * as from'mongoose';从“ ../_settings”导入{设置};导出const FeatureSchema = new mongoose.Schema({feature_name:{...

回答如下:

.populate('Features.feature_id')替换.populate('feature_id')

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论