如果模式相互引用,我可以从另一个文档中提取值吗?

Can I pull a value from another document, if the schemas reference each other?

我有一个职位发布集合和一个求职者集合。 申请人模式引用 MongoDB 中的工作职位,但是当我将其拉入我的 Angular 8 前端 HTML 时,我想在 applicant.component.html 中显示 postTitle。

有人告诉我使用 mongoose findOne(),但他们没有提到将代码放在哪里,将它放在 applicant.component.ts 中的任何地方都给我错误消息。

如何将其写入 applicant.component.ts 或添加到我的路由文件以使其正常工作?

两种模式

Post

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>

TS

export class ApplicantDetailsComponent implements OnInit {

    applicant: Applicant = { 
      id: null, 
      appPhone: '',
      appEmail: '',
      appName: '', 
      appAddress1: '',
      appAddress2: '',
      post: '', 
      appResume: '',
      updated: null 
    };

  isLoadingResults = true;
  post: Post[] = [];

  constructor(private route: ActivatedRoute, private api: ApplicantService, private router: Router, private postApi: PostService) { }

  ngOnInit() {
    this.getPost()
    this.getApplicantDetails(this.route.snapshot.params.id);
  }

  getApplicantDetails(id: any) {
    this.api.getApplicant(id)
      .subscribe((data: any) => {
        this.applicant = data;
        this.applicant.id = data._id;
        console.log(this.applicant);
        this.isLoadingResults = false;
      });
  }

  deleteApplicant(id: any) {
    this.isLoadingResults = true;
    this.api.deleteApplicant(id)
      .subscribe(res => {
        this.isLoadingResults = false;
        this.router.navigate(['/applicant']);
      }, (err) => {
        console.log(err);
        this.isLoadingResults = false;
      }
      );
  }

  getPost() {
    this.postApi.getPost(this.post)
      .subscribe((res: any) => {
        this.post = res._id;
        console.log(this.post);
        this.isLoadingResults = false;
      }, err => {
        console.log(err);
        this.isLoadingResults = false;
      });
  }


}

申请途径

router.get('/', passport.authenticate('jwt', { session: false}), function(req, res) {
  var token = getToken(req.headers);
  if (token) {
    Applicant.find(function (err, applicants) {
      if (err) return next(err);
      res.json(applicants);
    });
  } else {
    return res.status(403).send({success: false, msg: 'Unauthorized.'});
  }
});

router.get('/:id', passport.authenticate('jwt', { session: false}), function(req, res, next) {
var token = getToken(req.headers);
if (token) {
  Applicant.findById(req.params.id, function (err, applicant) {
    if (err) return next(err);
    res.json(applicant);
  });
} else {
  return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
});

router.post('/', passport.authenticate('jwt', { session: false}), function(req, res, next) {
var token = getToken(req.headers);
if (token) {
  Applicant.create(req.body, function (err, applicant) {
    if (err) return next(err);
    res.json(applicant);
  });
} else {
  return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
});

router.put('/:id', passport.authenticate('jwt', { session: false}), function(req, res, next) {
var token = getToken(req.headers);
if (token) {
  Applicant.findByIdAndUpdate(req.params.id, req.body, function (err, applicant) {
    if (err) return next(err);
    res.json(applicant);
  });
} else {
  return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
});

router.delete('/:id', passport.authenticate('jwt', { session: false}), function(req, res, next) {
var token = getToken(req.headers);
if (token) {
  Applicant.findByIdAndRemove(req.params.id, req.body, function (err, applicant) {
    if (err) return next(err);
    res.json(applicant);
  });
} else {
  return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
});

getToken = function (headers) {
if (headers && headers.authorization) {
  var parted = headers.authorization.split(' ');
  if (parted.length === 2) {
    return parted[1];
  } else {
    return null;
  }
} else {
  return null;
}
};

module.exports = router;

也试过了

HTML

<div class="col-md-6">
        <h3>{{applicant.appName}}</h3> 
        <h4 *ngIf="{{post._id}} === {{applicant.post}}, show">Applying for: {{post.postTitle}} </h4>
        <h4>Submitted: {{applicant.updated | date: 'dd MMM yyyy'}}</h4>
    </div>

答案是从我的 applicant.js 路由文件夹中获取帖子

router.get('/:id', passport.authenticate('jwt', { session: false }), function (req, res, next) {
  var token = getToken(req.headers);
  if (token) {
    Applicant.findById(req.params.id, function (err, applicant) {
      if (err) return next(err);
      console.log(applicant.post);
      Post.findOne({_id: applicant.post}).exec(function (err, post) {
        if (err) return next(err);
        console.log(post);
        res.json([applicant, post]);
      });

    });
  } else {
    return res.status(403).send({ success: false, msg: 'Unauthorized.' });
  }
});