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);
} } }; }
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With