【问题标题】:Mongoose Virtual not working猫鼬虚拟不工作
【发布时间】:2022-02-23 07:22:34
【问题描述】:

我有一个 mongoose Schema 调用用户名,我正在尝试在虚拟中连接用户名。当我在 get 中记录 virtual 或在这些模型中执行函数时,它返回未定义。

我参考了文档,其中示例是嵌套模型,我更喜欢当前模型而不是嵌套它。 我希望任何机构都可以帮助我。

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);

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    试试这个:

    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 会话中看到全名,但是当我明确称它为“取消定义取消定义”时。我使用猫鼬作为我的 ORM。
    • 您是否保存了您的用户?我已更新我的答案以显示这一点。
    • "UserSchema.set('toJSON', { virtuals: true })" 是我所缺少的
    • 只是把它放在那里:Moongose 5.8.11 不支持带有 getter(可能还有 setter)的箭头函数。
    【解决方案2】:

    我在使用虚拟时遇到了同样的问题,返回“未定义”。 我通过不使用箭头函数解决了这个问题(感谢@Bradyo 的评论)。 所以 Mongoose 5.12.7 也不支持箭头函数。

    【讨论】:

    • 用例子解释你的答案。它赋予了更多的意义。 check here
    • 为我解决了这个问题。正在使用箭头功能,想知道为什么“这个”没有得到解决。啊!忘了这甚至是一个问题。谢谢。
    【解决方案3】:

    试试这个:

    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);
    

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 2018-07-22
      • 2017-04-04
      • 1970-01-01
      • 2018-05-02
      • 2017-05-26
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多