猫鼬:从外键获取数据

时间:2019-12-19 10:40:12

标签: node.js mongoose

我有一个constructor( private fb: FormBuilder, private amen: AmenService, private router: Router ) { } ngOnInit() { this.getUserDetails(); this.updateForm = this.fb.group({ name: ['', Validators.required], email: ['', [Validators.required, Validators.email]], mobile: ['', Validators.required], user_img: [''] }); } getUserDetails() { this.amen.getUserDetails(this.params).subscribe( (response) => { this.updateForm.patchValue(this.handleResponse(response)); }, (error) => { console.log('Error: ', error); } ); } handleResponse(response) { if (response.status === 1) { return response.responseData.UserData; } if (response.status === 0) { this.forAlertBox = true; this.msg = response.msg; } } preview(event) { if (event.target.files.length > 0) { this.selectedImage = event.target.files[0].name; } } onSubmit() { this.submitted = true; // return flow if form is invalid if (this.updateForm.invalid) { return; } const formData = new FormData(); formData.append('user_img', this.selectedImage); formData.append('name', this.updateForm.get('name').value); formData.append('email', this.updateForm.get('email').value); formData.append('mobile', this.updateForm.get('mobile').value.internationalNumber); this.amen.updateUserProfile(formData).subscribe( (response) => { this.handleUpdateResponse(response); }, (error) => { console.log('after update error', error); } ); } handleUpdateResponse(response) { if (response.status === 1) { this.router.navigate(['edit-my-profile']); } if (response.status === 0) { this.forAlertBox = true; this.msg = response.msg; } } ,其中两个ObjectId都作为followingModel的外键

userModel

我可以要求某些用户名的所有关注者

import * as mongoose from 'mongoose';
import { Document, Schema } from 'mongoose';

interface FollowingModelInterface extends Document {
  followingId: string;
  userId: string;
}

const followingSchema: Schema = new Schema(
  {
    followingId: { type: mongoose.Types.ObjectId, index: true  }, // <---- Here
    userId: { type: mongoose.Types.ObjectId, index: true }, // <---------- Here
  },
  {
    timestamps: true,
  }
);

const followingModel = mongoose.model<FollowingModelInterface>('Following', followingSchema);

export { followingModel, FollowingModelInterface };

以数组形式返回正确的值, static followers = async (req: Request, res: Response) => { try { const { username } = req.body; const getAccount = await userModel .findOne({ 'shared.username': username }) .exec(); if (!getAccount) throw new Error('User not found'); const getFollowers = await followingModel .find({ userId: getAccount._id }) // <----------------- Here .exec(); console.log(getFollowers); } catch (error) { res.status(400).send({ message: 'Server Error', error }); } }; followingId中的外键

userModel

对于数组中的每个项目,我如何让用户[ { _id: 5dfa79cba80ccbfe26c8ef97, followingId: 5dfa73ec789ec45f882ebab4, // <----------------- Here userId: 5dfa7127536a675cc1ca29d4, __v: 0, createdAt: 2019-12-18T19:11:07.035Z, updatedAt: 2019-12-18T19:11:07.035Z } ] 参与对mongo的相同调用?可以在fullName

的userModel中找到fullName

我不想遍历数组,从而为数组中的每个文档调用用户模型

以下内容有效,但速度慢...

shared.fullName

0 个答案:

没有答案