简体   繁体   中英

module.export and global objects

I'm confused.

Occasionally when my web api receives data it mixes the data up between objects and it appears to me that the global object in the file is actually being persistent..

Here is the basic layout of the code

handlers.js

const something = require('./otherfile')
let handlers ={}

handlers.customers = function (data, callback) {
  let acceptableMethods = ['post'];
  if (acceptableMethods.indexOf(data.method) > -1) {
    handlers._customers[data.method](data, callback);
  } else {  
    callback(405); 
  }
};

handlers._customers = {}

handlers._customers.post = async function (data, callback) {
  customer.new(data);
  callback(200, 'success')
}

otherfile.js

let contacts_list = [];
let accountData = {};

module.exports = something = {
  new: {
      dostuff: async function (data) {
      // update and reference global objects here..
      accountData.name = data.name;
      accountData.otherProperty = await somefunction(data.prop)
    }
  }
}

I expected that since it is requiring an exported module that each time it would call the exported module it would be treated as its own object, however, it seems that the object is not being treated as unique and is instead being overwritten in part and 'randomly'. This suggests to me that I may be able to export a mutating object such as an array across files

Am I correct in that the global is being persisted across multiple requests? Would setting the global within the export object affect the behaviour of this object in any way? In this case I don't want this data to mutate.

Thanks in advance for your constructive criticisms and guidance :)

[Restructure your code so you are creating a new object on every request. Module's are cached on the first require so all of your variables and object properties will be persisted across calls.

// handler.js



const somethingFactory = require('./otherfile')

module.exports = function(){
  let handlers = {}
  const something = somethingFactory();

  handlers.customers = function (data, callback) {
    let acceptableMethods = ['post'];
    if (acceptableMethods.indexOf(data.method) > -1) {
      handlers._customers[data.method](data, callback);
    } else {  
      callback(405); 
    }
  };

  handlers._customers = {}

  handlers._customers.post = async function (data, callback) {
    customer.new(data);
    callback(200, 'success')
  }

  return handlers;

};

otherfile.js

module.exports = function(){
  let contacts_list = [];
  let accountData = {};

  return {
     new: {
      dostuff: async function (data) {
      // update and reference global objects here..
      accountData.name = data.name;
      accountData.otherProperty = await somefunction(data.prop)
     }
    }
  }

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM