Javascript`the`不断变化,不知道为什么

时间:2016-04-07 14:58:16

标签: javascript redis this

目前,我正在使用Redis和Caminte在Linux机器上的Node.js 5.2.0上编写应用程序。在尝试向数据库对象添加不同的原型方法时,this所指的内容在我们的引用中不断变换。在modelRules.js中调用push后,this会移动类型。我正在寻求一些帮助:

  1. 如何在模块本身之外始终引用特定模块(接受模式对象的函数)的实例化。我想将原型函数(如addModelBelongsTo)添加到User对象中,遗憾的是,当引用类中的内部可修改数据成员时,我的函数似乎就会中断。
  2. 原型存取器的正确组织。在引用这些类的实例化内部时是否应该使用特定样式?
  3. 为什么类User的实例化会在类的多个实例中保持数据?对于self[restructuredModelName](数组类型),每当我在一个实例上调用此方法时,另一个对象的另一个实例化已经包含第一个实例化的数据。这不应该发生。
  4. user.js的

    module.exports = function (schema) {
      const IBANVerificationStatusSymbol = Symbol('IBANVerificationStatus');
      const relationalMapper = require('./../relationalMapper');
    
      const userObj = {
        id: { type: schema.Number },
        firstName: { type: schema.String },
        lastName: { type: schema.String },
        IBAN: { type: schema.String, unique: true },
        IBANVerified: { type: schema.Boolean, default: false },
        IBANVerificationCode: { type: schema.String },
        BIC: { type: schema.String },
        email: { type: schema.String, index: true, unique: true },
        password: { type: schema.String },
        status: { type: schema.Number, default: 0 },
        profilePicture: { type: schema.String },
        phone: { type: schema.String, index: true, unique: true },
        accessToken: { type: schema.String },
        prefix: { type: schema.String, default: '+31' },
        updated: { type: schema.Date, default: Date.now() },
        created: { type: schema.Date, default: Date.now() },
        lastAccessedFeed: { type: schema.Date },
        street: { type: schema.String },
        streetNumber: { type: schema.String },
        postalCode: { type: schema.String },
        city: { type: schema.String },
        country: { type: schema.String },
        FCR: { type: schema.Number, default: 0 },
      };
    
      // There's GOTTA be a way to typecheck at compilation
      const associationMap = {
        Activity: { belongsTo: 'Activity', hasMany: 'activities' },
        Client: { belongsTo: null, hasMany: 'clients' },
        Group: { belongsTo: 'Group', hasMany: 'groups' },
        Mandate: { belongsTo: null, hasMany: 'mandates' },
        Transaction: { belongsTo: null, hasMany: 'transactions' },
        Update: { belongsTo: null, hasMany: 'updates' },
        Reaction: { belongsTo: null, hasMany: 'reactions' },
      };
    
      relationalMapper.createRelations(associationMap, userObj, schema);
    
      const User = schema.define('user', userObj, {
    
      });
    
        const setId = function (self, models) {
          //  self.addClients(1);
        };
    
        User.hooks = {
          afterInitialize: [setId],
        };
    
        User.prototype.obj = userObj;
    
        User.associationMap = associationMap;
    
        User.prototype.associationMap = associationMap;
    
        return User;
    };
    

    modelRules.js:

    function addModelBelongsTo(modelName, models, modelObjKey, modelRelated) {
        const restructuredModelName = `memberOf${modelName}`;
        const restructuredModelNameCamel = `addMemberOf${modelName}`;
        const currentModels = models;
    
        currentModels[modelObjKey].prototype[restructuredModelNameCamel] = function(modelId) {
            const self = this;
            return new Promise((resolve, reject) => {
                if (self[restructuredModelName].indexOf(modelId) <= -1) {
                    modelRelated.exists(modelId, function(err, exists) {
                        if (err || !exists) { reject(new Error(err || 'Doesnt exist')); }
                        console.log(`This:${self}\nrestructuredModelName:${JSON.stringify(self[restructuredModelName])}`);
                        self[restructuredModelName].push(modelId);
                        console.log(`This:${self}\nrestructuredModelName:${restructuredModelName}`);
                        self.save((saveErr) => {
                            saveErr ? reject(new Error(saveErr)) : resolve(self);
                        });
                    });
                } else {
                    reject(new Error(''));
                }
            });
        };
    }
    

0 个答案:

没有答案