async/await 不使用 mongoose 实例方法

async/await not working with mongoose instance methods

我正在尝试创建需要存储路径的用户 mongoose 文档。我想等待创建目录和解析路径。但它不起作用,保存用户后,user.storagePath 仍未定义。请找出问题。

createStorage()的代码如下

const getMountRoot = require('../configuration/configuration').getMountRoot
const path = require('path')
const fs = require('fs')
const Logger = require('../configuration/configuration').getLogger

module.exports = (email, firstName, secondName) => {
  email = String(email).toLowerCase().replace(/[@_\.\-]/g, '')
  firstName = String(firstName).toLowerCase().replace(/[@_\.\-]/g, '')
  secondName = String(secondName).toLowerCase().replace(/[@_\.\-]/g, '')

  let storagePath = path.join(getMountRoot(), `${secondName}${email}${firstName}`)
  return fs.promises.mkdir(storagePath, { recursive: true })
    .then(() => { return storagePath })
    .catch(() => {Logger.log(err); return storagePath})
}

下面是实例方法

const createStorage = require('../extra/create-storage')

userSchema.methods.createStorage = async function() {
  this.storagePath = await createStorage(this.email, this.firstName, this.secondName)
}

请注意,在调用 save()

之前,我在 User 实例上调用了 createStorage()

正如@qqilihq 所想,我需要等待实例方法调用。这样做是正确的。