javascript - Mongoose 方法中这个变量的上下文是什么?

标签 javascript node.js mongoose this

在下面的代码中,我看到“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);
});

最佳答案

查看documentation

In model middleware functions, this refers to the model.

<小时/>

In document middleware functions, this refers to the document.

<小时/>

In query middleware functions, this refers to the query.

<小时/>

Aggregate middleware executes when you call exec() on an aggregate object. In aggregate middleware, this refers to the aggregation object.

<小时/>

this 的目的是让您可以与数据交互。比如改变里面的一个值或者做其他处理。

关于javascript - Mongoose 方法中这个变量的上下文是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50173450/

相关文章:

javascript - Mongoose 批量保存/异步更新每次只更新一些文档

node.js - 用 sinon 模拟/ stub Mongoose findById

javascript - twilio错误 "accountSid must start with AC"

javascript - 将自动播放添加到此 JS slider

javascript - Jquery:输入模糊

javascript - 没有 href 的 GetElementsByTagName

javascript - 在 Node 脚本中加载预编译的 Handlebars 模板?

node.js - 通过 gRPC 从麦克风流式传输音频

javascript - 按最高计数聚合

node.js - NodeJS Express 和 Mongoose : How to get data from array not in one but in multi table columns