我想在默认情况下将mongoose中的created_by字段设置为自动生成功能。但是我正面临麻烦。我无法将req.body.username设置为默认状态,我也已经尝试创建变量,并称它仍然不起作用。这是我在模型上的代码:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const { findByUsername } = require('../services/userService');
const bcrypt = require('bcrypt');
const username = req.body.username;
created_by: {
type: String,
required: [true, "This field is required"],
default: username
},
updated_by: {
type: String,
required: [false, "This field is required"]
}
}, {collection: 'ticket'});
const Ticket = mongoose.model("Ticket", TicketSchema);
module.exports = {Ticket, TicketSchema};
我想要(created_by)字段从用户名接收值,我已经尝试通过controller.js及其工作来获取用户名值。所以我想根据用户当前登录的用户名来设置created_by字段的默认值。
这是我已经在controller.js中尝试过的代码
module.exports = {
GetUsername : async function (req, res, next) {
errors = {};
const username = req.body.username;
return res.status(200).json({"status": "success", "data": username});
}
}
有人可以告诉我如何在猫鼬模式中获取req.body.user吗?