【发布时间】: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吗?... -
我看不到任何代码