首页 > 解决方案 > 使用公共参数将 ID 从一个集合拉到另一个集合

问题描述

好的,所以我对 Angular 还很陌生,我的应聘者集合引用了带有 ID 的职位发布集合,

但是如何使用与帖子集合的 id 参数匹配的申请人.post 参数来拉出 postTitle?

两种模式

邮政

var PostSchema = new mongoose.Schema({
  category : { type: Schema.Types.ObjectId, ref: 'Category' },
  id: String,
  postTitle: String,
  postAuthor: String,
  postDescription: String,
  postQualifications: String,
  postReference: String,
  updated: { type: Date, default: Date.now },
});

申请人

var ApplicantSchema = new mongoose.Schema ({
    post : { type: Schema.Types.ObjectId, ref: 'Post' },
    id: String,
    appName: String,
    appPhone: String,
    appEmail: String,
    appAddress1: String,
    appAddress2: String,
    appResume: String,
    updated: { type: Date, default: Date.now }
});

HTML

<div class="button-row">
        <a mat-flat-button color="primary" [routerLink]="['/applicant']">Back</a>
      </div>
      <hr>
    <div class="row application">
        <div class="col-md-6">
            <h3>{{applicant.appName}}</h3> 
            <h4>Applying for: {{applicant.post}} || Need post.postTitle </h4>
            <h4>Submitted: {{applicant.updated | date: 'dd MMM yyyy'}}</h4>
        </div>
        <div class="col-md-5">
            <h4>{{applicant.appPhone}}</h4>
            <h4>{{applicant.appEmail}}</h4>
            <h4>{{applicant.appAddress1}}, </h4>
            <h4>{{applicant.appAddress2}}</h4>
        </div>
        <div class="col-md-1">
            <a class="btn btn-block" button (click)="deleteApplicant(applicant.id)">
                <mat-icon>delete</mat-icon>
            </a>
        </div>
        <br>
        <div class="col-md-12">
            <p innerHTML={{applicant.appResume}}></p>
        </div>
    </div>

标签: angularmongodbangular8

解决方案


您必须在您的 node.js 应用程序中进行更改,使用 mongoose 人口并在您的路由器文件中填充 Post 模型,您将在其中根据 id 过滤申请人,如下所示:-

Applicant.findOne({id: req.applicant.id})
.populate('post')
.then(res => {// postTitle will be accessible here})

这里申请人是你的模特。

参考猫鼬填充


推荐阅读