Mongoose虚拟财产中的访问请求

时间:2015-07-22 09:00:35

标签: node.js express mongoose

考虑这个样本

var ArticleSchema = new Schema({
    content: {
        type: String,
        default: '',
        trim: true
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
}, {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
});
ArticleSchema.virtual('is_owner').get(function () {
    //how to access app request here?
    //i would like to get req.user variable here
    return false;
});
mongoose.model('Article', ArticleSchema);

如评论中所述,我想访问虚拟财产内的路径请求,但不知道是否合法?

第一个想法是要求明确内部虚拟

ArticleSchema.virtual('is_owner').get(function () {
        var express = require('express');
        var app = express();
        //then what?
    });

但是app没有请求

1 个答案:

答案 0 :(得分:0)

让我们说这是你的路线:

ArticleSchema.virtual('is_owner').set(function (req) {
  // => do what ever you want here.
});

这是你的虚拟:

{{1}}