Alvin
Alvin

Reputation: 8499

Pass variables into exports object

I have two files, how can I pass the value to variable?

sample.js:

module.exports = {
   content: [
    {
        table: {
            body: [
                [
                    { text: address, alignment: 'center'}
                ]
        }
    }
}

app.js:

var sample = require('sample');

How to pass the address into the sample object?

Upvotes: 0

Views: 660

Answers (4)

Gab
Gab

Reputation: 8323

function getContent(adress) {
  return content: [
    {
      table: {
          body: [
              [
                { text: address, alignment: 'center'}
              ]
        }
    }
}

module.exports = getContent;

app.js:

var sample = require('./sample')(adress);

Upvotes: 1

Harshit Thukral
Harshit Thukral

Reputation: 1

You can make module.exports to be a function:

sample.js

module.exports = (address) => { return { content: [
  {
    table: {
      body: [
        {text: address, alignment: 'center'}
      ]
    }
  }
] };}

app.js

var sample = require('sample')('your address parameter');

Upvotes: 0

Sebastian Tilch
Sebastian Tilch

Reputation: 79

Try this:

module.exports = (address) => {
  return {
    content: [
      {
        table: {
          body: [
            {text: address, alignment: 'center'}
          ]
        }
      }
    ]
  }
}

var sample = require('sample')('The address');

Upvotes: 0

baao
baao

Reputation: 73221

You want to export a function that returns an object:

module.exports = function (address) {
    return {
        content: [
            {
                table: {
                    body: [
                        {
                            text: address,
                            alignment: 'center'
                        }
                    ]
                }
            }
        ]
    }
};

Now you can fill the values:

var sample = require("./sample");
console.log(sample("fooAddress"));

Upvotes: 1

Related Questions