【发布时间】:2021-07-12 11:34:39
【问题描述】:
我正在尝试使用 mongoose 和 nodejs 获取候选人或 HR(用户角色)对象。我有一个用户,两个角色都是从它派生的。
尝试使用唯一用户名和密码进行连接时。结果将发送一个用户对象。我还想发送与该用户相关联的候选人/或 HR。
我通过引用候选/HR 模式来传递用户对象:
const candidateSchema = new Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
index: true,
},
fullName: String,
profilePhoto: String,
birthday: Date,
我需要在 exec() 函数中获取用户的候选对象。将其保存在变量中并将其作为 res 发送到登录函数
app.post("/api/auth/signin", (req, res) => {
User.findOne({
username: req.body.username,
})
.populate("roles", "-__v")
.exec((err, user) => {
if (err) {
res.status(500).send({ message: err });
return;
}
const candi = candidat.findOne({ user: user }).exec((err, candidate) => {
//I want to save the candidate var
}));
//console.log("res",candi);
.....
});
【问题讨论】: