我正在尝试创建需要存储路径的用户猫鼬文档。我想等待目录创建和路径解析。但是它不起作用,并且在保存用户后,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)
}


请注意,在调用createStorage()之前,我先在User实例上调用save()

最佳答案

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

07-24 18:18