mongoose虚拟字段不起作用。

4

我有一个名为用户名的mongoose Schema,我正在尝试在虚拟环境中连接用户名。但是当我在这个模型中记录虚拟结果或者执行函数时,它返回undefined。

我已经参考了文档和他们嵌套模型的示例,但我更喜欢当前模型而不是嵌套它。希望有人能帮助我解决这个问题。

const Schema = mongoose.Schema;
const UserSchema = new Schema({ 
firstname: {
type: String,
lowercase: true,
required: true
},
lastname: {
type: String,
lowercase:true,
text: true,trim: true,
required: true},
middlename: {
type: String,
text: true,trim: true,
lowercase: true
 }
  UserSchema.virtual('fullname')
.get(function(){
  return this.firstname + ' ' + this.lastname;
})
.set(function(firstname, lastname) {
  firstname = this.firstname;
  lastname = this.lastname;
});
UserSchema.set('toObject', {virtuals: true});
UserSchema.set('toJSON', {virtuals: true});
  });
  const User = module.exports = mongoose.model('User', UserSchema);
3个回答

19

试试这个:

const Schema = mongoose.Schema
const UserSchema = new Schema({ 
  firstname: {
    type: String,
    lowercase: true,
    required: true
  },
  lastname: {
    type: String,
    lowercase:true,
    text: true,
    trim: true,
    required: true
  }
})

UserSchema.set('toObject', { virtuals: true })
UserSchema.set('toJSON', { virtuals: true })

UserSchema.virtual('fullname')
  .get(function() {
    return this.firstname + ' ' + this.lastname
  })
  .set(function(newName) {
    var nameParts = newName.split(' ')
    this.firstname = nameParts[0]
    this.lastname = nameParts[1]
  })

const User = module.exports = mongoose.model('User', UserSchema)

然后,您应该能够这样做:

var somebody = new User({
  firstname: 'Some',
  lastname: 'Body'
})

somebody.save(function(err, doc) {
  console.log(somebody.fullname) // Some Body

  doc.fullname = 'Another Person'

  doc.save(function(err, doc) {
    console.log(doc.fullname) // Another Person
  })
})

我希望这能有所帮助。


非常感谢。我注意到在req.user会话中可以看到fullname,但是当我显式调用它时,它会写入“undefine undefine”。我正在使用mongoose作为我的ORM。 - Kabiru Wahab
你保存了用户吗?我已更新我的答案以显示此内容。 - Steve Holgado
我所缺少的是"UserSchema.set('toJSON', { virtuals: true })"。 - Ryan Knell
5
提一句:Moongose 5.8.11不支持使用箭头函数来定义getter(和presumably setters)。 - Bradyo

6

我也遇到了virtual返回“undefined”的问题。感谢@Bradyo的评论,我通过不使用箭头函数解决了这个问题。所以Mongoose 5.12.7也不支持箭头函数。


1
用例子解释你的答案会更有意义。在这里查看 - Shashank Gb
解决了我的问题。我正在使用箭头函数,不知道为什么'this'没有被解析。唉!忘记这是个问题了。谢谢。 - Tayab

0

试一下:

const Schema = mongoose.Schema;

const UserSchema = new Schema(
    {
        firstname: {
            type: String,
            lowercase: true,
            required: true
        },
        lastname: {
            type: String,
            lowercase: true,
            text: true, trim: true,
            required: true
        },
        middlename: {
            type: String,
            text: true, trim: true,
            lowercase: true
        },
    },
    {
        toJSON: { virtuals: true },
        toObject: { virtuals: true },
    }
);
UserSchema.virtual('fullname').get(function(){
    return this.firstname + ' ' + this.lastname;
}).set(function(firstname, lastname) {
    firstname = this.firstname;
    lastname = this.lastname;
});

const User = module.exports = mongoose.model('User', UserSchema);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接