首先我不知道这是可能的,但是我试图从此处传递param:
await new User(req.body, {lang: "lang"});
在这里获取它:
const UserSchema = new Schema({
name: {
type: String,
trim: true,
required: validationMessagesTranslation("name", "here fetch param")
}
})
答案 0 :(得分:0)
在猫鼬模块中,您可以看到类似的内容,
Model(doc?: any): any
values with which to create the document
Model constructor Provides the interface to MongoDB collections as well as creates document instances.
@event
error If listening to this event, it is emitted when a document was saved without passing a callback and an error occurred. If not listening, the event bubbles to the connection used to create this Model.
@event
index Emitted after Model#ensureIndexes completes. If an error occurred it is passed with the event.
@event
index-single-start Emitted when an individual index starts within Model#ensureIndexes. The fields and options being used to build the index are also passed with the event.
@event
index-single-done Emitted when an individual index finishes within Model#ensureIndexes. If an error occurred it is passed with the event. The fields, options, and index name are also passed.
但是,您可以在对象内设置参数,并使用 Mongoose Middleware 使用此参数执行所需的操作,例如schema.pre('save', fn)
。 Here是有关中间件的文档页面。
//### app.js ###
.
.
.
let object = { name: 'Daniel', lang: 'lang' };
let userMongooseObject = new User(object);
await userMongooseObject.save();
.
.
.
//### userModel.js ###
let userSchema = new Schema(...);
schema.pre('save', function(next) {
// do stuff
next();
});