Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call the function from other node js file

These codes are stored in separate file and I tried to call the get method from this file to another nodejs, but I am getting only [Function] as a out put.

Can any one tell me how to call the get method from this file to another node js file

'use strict';
var createAPIRequest = require('../../lib/apirequest');
function Fitness(options) {
  var self = this;
  this._options = options || {};
  this.users = {
    dataSources: {
      get: function(params, callback) {
        var parameters = {
          options: {
            url: 'https://www.googleapis.com/fitness/v1/users/{userId}/dataSources/{dataSourceId}',
            method: 'GET'
          },
          params: params,
          requiredParams: ['userId', 'dataSourceId'],
          pathParams: ['dataSourceId', 'userId'],
          context: self
        };
        return createAPIRequest(parameters, callback);
      }     }   }; }
like image 923
VG__ Avatar asked Feb 17 '16 13:02

VG__


People also ask

Can we call function from one js file to another?

As long as both are referenced by the web page, yes. You simply call the functions as if they are in the same JS file.

How do you call a function in node JS?

A function can be executed by calling the execute() method in which the function ID and configuration (of type JSON) are passed as parameters. The functions reference used in the code snippets below is the component instance.

How do you make a function or a value available to node js applications that import your module?

Inside each module, therefore, 'module' refers to the object representing the current module. This object holds metadata about the module, such as the filename of the module, as well as the module's id. In Node. js, the practice of making a module's code available for other modules to use is called "exporting" values.


2 Answers

In this file you add

module.exports = Fitness

Then where you want to use it you do

var Fitness = require('./fitness');

like image 148
akc42 Avatar answered Sep 27 '22 23:09

akc42


Very first thing I noted is 'dataSources' property is inside 'users' object.So you need to do users.dataSources from outside to access this object.

To make things work.

I have made some changes in fitness.js

'use strict';
var createAPIRequest = require('../../lib/apirequest');

function Fitness(options) {
    var self = this;
    this._options = options || {};
    this.users = {
    dataSources  : { // You have property 'dataSources' in users object that will be accessible via Fitness object(Publically)
        get: function(params, callback) {
          var parameters = {
              options: {
                url: 'https://www.googleapis.com/fitness/v1/users/{userId}/dataSources/{dataSourceId}',
                method: 'GET'
              },
              params: params,
              requiredParams: ['userId', 'dataSourceId'],
              pathParams: ['dataSourceId', 'userId'],
              context: self
            };
            return createAPIRequest(parameters, callback);
         }    
       }   
    }; 
}

module.exports = Fitness; // This will export your Fitness constructor

Now write a below code to access Fitness module in another file

var Fitness = require('pathToFitness/fitness.js');  // This will load your fitness module
var fitness = new Fitness(options); // Create object of class Fitness
fitness.users.dataSources.get();  // Access get() method 
like image 25
Piyush Sagar Avatar answered Sep 27 '22 22:09

Piyush Sagar