Skip to content

MongoDB

使用 Mongoose 驱动的 MongoDB。

踩坑记录

This 为空的问题

在 schema 中定义如下的 hook 函数,实际使用时却获取不到 this 的值。

js
const schema = new mongoose.Schema({
  password: String,
})

schema.pre('save', next => {
  next()
})

const Model = mongoose.model('Test', schema)

const test = new Model({
  password: 'testpass',
})

test.save()

解析:箭头函数会维持 this 的指向,而这里我们需要的是 model 的实例。改成非剪头函数,即可正常。

参考链接:The “this” object is empty in pre('save')

CC BY-NC 4.0