javascript - Mongoose 方法中这个变量的上下文是什么?
问题描述
在下面的代码中,我看到“this”在 Mongoose 的链接方法中使用。所以,我不知道'this'的上下文是什么以及使用'this'的目的是什么?
PersonSchema
.virtual('name.full')
.get(function () {
return this.name.first + ' ' + this.name.last;
})
.set(function (setFullNameTo) {
var split = setFullNameTo.split(' ')
, firstName = split[0]
, lastName = split[1];
this.set('name.first', firstName);
this.set('name.last', lastName);
});
解决方案
看文档
在模型中间件函数中,this 指的是模型。
在文档中间件函数中,this 指的是文档。
在查询中间件函数中,this 指的是查询。
当您在聚合对象上调用 exec() 时,将执行聚合中间件。在聚合中间件中,this 指的是聚合对象。
的目的this
是您可以与数据进行交互。比如改变里面的一个值或者做其他的处理。
推荐阅读
- javascript - 导航菜单翻译带有溢出隐藏的网页时水平滚动
- c - 集成到静态库中的可重定位对象列表
- amazon-s3 - 使用 Cloudfront/S3 根据 AWS 上的当前状态显示不同的网站
- python - 在 python-pptx 中为运行添加突出显示
- primeng - 动态对话框上的页脚按钮
- mime-types - 正确的数据 URL 的 MIME 类型
- r - 为什么 case_when 在 mutate 中工作时没有?
- exceljs - ExcelJS:没有正确保存文件
- go - Golang 的 LDAP 客户端库如何使用证书?
- asp.net-core - 更改基于配置文件引用的 NuGet 包