【问题标题】:Result undefined JS. I can't acess this atribute结果未定义的 JS。我无法访问此属性
【发布时间】:2020-03-23 03:16:27
【问题描述】:

我无法得到 avatar.path 的结果,我该怎么做? 如果我放this.nome,我可以正常接收,但是我不能得到avatar.path的结果。

如果我写this.avatar.name = value,则会抛出以下错误:

(节点:6652)UnhandledPromiseRejectionWarning:TypeError:无法读取 未定义的属性“路径”

我需要获取avatar.path,因为我将用于返回 URL

const UserSchema = mongoose.Schema(
  {
    nome: {
      type: String,
      require: true
    },
    email: {`enter code here`
      type: String,
      unique: true,
      required: true,
      lowercase: true
    },
    password: {
      type: String,
      required: true
    },
    passwordresetoken: {
      type: String,
      select: false
    },
    passwordresetexpires: {
      type: Date,
      select: false
    },
    usuario_validado: {
      type: Boolean
    },
    cpf: {
      type: String,
      required: true
    },
    user_cuidador: {
      type: Boolean
    },
    avatar: [
      {
        nome: {
          type: String
        },
        path: { type: String, required: true }
      }
    ],
    createdAt: {
      type: Date,
      default: Date.now
    }
  },
  {
    toObject: {
      getters: true,
      setter: true,
      virtuals: true
    },
    toJSON: {
      getters: true,
      setter: true,
      virtuals: true
    }
  }
);


UserSchema.virtual("url").get(function() {
  console.log(this.nome); // I receive right the name
  console.log(this.avatar); // I receive undefined

  // I need to return avatar.path 
  return `http://localhost:300/files/${this.nome}`; // I can receive all the names right. But, I need avatar.path
});

【问题讨论】:

  • 嘿.. 你能在第 7 行看到enter code here 吗?...
  • 我看不到任何代码

标签: node.js mongoose


【解决方案1】:

avatar 是一个数组

{
  nome: {
    type: String
  },
  path: { type: String, required: true }
}

定义
avatar: [
  {
    nome: {
      type: String
    },
    path: { type: String, required: true }
  }
],

也可以是undefined

您在这里将其视为对象:this.avatar.name = value

如果假设它总是一个1的数组,那么将它转换成一个对象:

avatar: {
    nome: {
      type: String
    },
    path: { type: String, required: true }
  },

并确保在您的虚拟机中检查为空/未定义。如果您需要头像始终存在,那么如果没有记错的话,您可以将头像设置为架构并要求它。

const AvatarSchema = mongoose.Schema({
  nome: {
    type: String
  },
  path: { type: String, required: true }
})

然后在你的UserSchema 中,你会引用它:

avatar: { type: AvatarSchema, required: true }

否则你需要空/未定义检查:

UserSchema.virtual("url").get(function() {
  console.log(this.nome); // I receive right the name
  console.log(this.avatar); // I receive undefined

  // if avatar is an array then I guess you just want the first one?
  let avatarPath;
  if(this.avatar && this.avatar.length) {
    avatarPath = this.avatar[0].path
  } 
  // ... 

  return avatarPath ? avatarPath : `http://localhost:300/files/${this.nome}`; 
});

如果您保持架构不变并尝试设置头像路径,那么您将执行以下操作:

this.avatar = this.avatar || []
this.avatar.push({name: value})
// but it will fail to validate because path is required.

查看mongoose subdocs的文档

编辑:

我忘了在你的问题中提到你,你指的是this.avatar.name,但你的架构指的是nome而不是name

【讨论】:

    猜你喜欢
    • 2018-02-16
    • 2023-03-20
    • 2021-11-18
    • 2019-05-27
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多