我正在 NodeJS、expressJS 中开发 API,并且遵循存储库模式。所以有控制器、服务和存储库。现在,根据存储库模式的规则,我的控制器、服务和存储库如下所示。这些仅用于演示目的,实际代码与此不同。
authController.js
export const loginController = async (req, res, next) => {
const user = await authServices.login(req.body);
if (user.error) {
res.error(user.errorBody)
}
else {
return res.success({ code: 200, message: "Logged in succesfully", data: user });
}
}
Run Code Online (Sandbox Code Playgroud)
authServices.js
export const loginServcies = async (loginDetails) => {
const user = await userRepository.login(loginDetails);
if (!user) {
return {
error: true, errorBody: {
code: 422, message: 'User doesnot exist', errors: null
}
}
}
else if (!user.active) {
return { …
Run Code Online (Sandbox Code Playgroud) 我在 MongoDB 中有一个作业集合,在这个集合中,文档中有一个名为应用用户的字段。我想在新用户申请此工作时更新此字段。因此,基本上此字段存储申请此工作的所有用户的 ID。
我正在使用 findOneAndUpdate() 函数,但无法做到。
Job.findOneAndUpdate({ _id: req.params.id }, { $set: { appliedUser:
req.user.id } }, function(err, job) {
console.log(job);
})
Run Code Online (Sandbox Code Playgroud)
这是我的架构:
const jobSchema = new Schema({
date: { type: Date, default: Date() },
expirydate: { type: Date, required: true },
name: { type: String, required: true },
companydetails: { type: String, required: true },
address: { type: String, required: true },
role: { type: String, required: true },
city: { type: String, required: true }, …
Run Code Online (Sandbox Code Playgroud)