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);
      }     }   }; }

In this file you add

module.exports = Fitness

Then where you want to use it you do

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


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 

get is an inner function that requires that the class is also created. In your module, you could export the whole class:

module.exports.Fitness = Fitness;

In your other module:

var f = require('./Fitness'); /* given that Fitness.js is the name */
var fitness = new f.Fitness(...); /* call module.exports.Fitness of required file */
fitness.users.dataSources.get(...);

Have you tried so? If yes, where exactly do you get an error?

链接地址: http://www.djcxy.com/p/97032.html

上一篇: 将javascript文件包含在node.js脚本中

下一篇: 如何从其他节点js文件中调用该函数