Tag: 静态方法

在后期保存中间件中使用静态方法

我有一个静态方法来执行一个find(),并在应用程序启动时将Campaign数据添加到Redis。 CampaignSchema.statics.activeCampaignsToRedis = function () { this .find() .where('active').equals(true) … }; 我想添加一个后保存钩子,当新的Campaign被添加或修改时,它会重新运行静态方法来更新Redis中的数据。 CampaignSchema.post('save', function (next) { // call CampaignSchema.statics.activeCampaignsToRedis(); });

我可以用Node中的“静态方法”来扩充Object,Function,Date等吗?

如果我创build一个Node.js模块“augs”包含 Object.foo = "bar"; 然后inputREPL require("./augs"); typeof Object.foo 我回到'undefined' 。 我们在我们的Web应用程序中有大量的代码依赖于添加到Object , Function , Date等方便的方法。我们试图在前端和后端之间共享一些代码,但是好像Node会重置这些构造函数,或以某种方式防止给定模块中的这些变化泄漏到其他模块中。 虽然这是非常聪明的,我很欣赏保护水平,有没有办法说“我知道我在做什么,请让我扩充Object ”?

mongoose – this.find()不存在

在我的模型中,我试图做一个静态的getUserByToken方法。 但是,如果我像文档中那样做的话,我就可以了 this.find is not a function 我的代码如下所示: 'use strict'; const mongoose = require('mongoose'); const Schema = mongoose.Schema; const schema = new Schema({ mail: { type: String, required: true, validate: { validator: (mail) => { return /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[az][az])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i.test(mail); } } }, birthDate: { type: Date, required: true, max: Date.now, min: new Date('1896-06-30') }, password: { type: […]