【问题标题】:get name of model/collection in mongoose pre save hook在猫鼬预保存钩子中获取模型/集合的名称
【发布时间】:2016-01-07 21:48:57
【问题描述】:

我正在创建一个猫鼬预保存挂钩,我想知道模型/集合的名称,我将其保存在预保存挂钩中。我可以从哪个变量中得到它。

module.exports = exports = function lastModifiedPlugin (schema, options) {
    schema.pre('save', function (next) {
        var self = this;

        self.constructor.findOne({ _id: self._id }, function (err, launch){
            console.log(" model " + self.mongooseCollection);
            console.log(Object.getOwnPropertyNames(this));
            console.log(Object.getOwnPropertyNames(schema));

            next()
        });

    })

    if (options && options.index) {
        schema.path('lastMod').index(options.index)
    }
}

我尝试探索 thisschema 变量的函数和属性,但无法保存名称 od model。

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

我们可以从:

self.constructor.modelName

【讨论】:

    猜你喜欢
    • 2013-08-14
    • 2018-03-04
    • 1970-01-01
    • 2012-08-08
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多