1

I have a problem. I want that in a function a variable is set and that is exported by another function as i don't want to return that variable at the invocation of function. How can i do this. Code look like this.

var details = {};
   module.exports.set = function (var1){ 
   details.n = var1;
}
module.exports.m = details.n;

But details.n is returning undefined. I know it is because of var details = {}. Then how can i resolve my purpose. Thanks in advance.

2 Answers 2

2

You should export details instead of details.n

module.exports.m = details;

This allow you to get the reference of details using import. Exporting details.n will get you only the initial value, updates won't get reflected.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you add little more about import with code? @ArunGhosh
1

This works, tried and tested

module.exports.set = function (var1) {
    module.exports.m = var1;
};

Now you can call set with anything and it will be set in 'm'

var test = require('./test');
test.set('test123');
console.log(test.m);
test.set('test1234');
console.log(test.m);
test.set(function() {});
console.log(test.m);

OUTPUT:

test123
test1234
[Function]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.