4

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);
      }     }   }; }
VG__
  • 513
  • 1
  • 6
  • 15
  • Are you posting the complete file? – bolav Feb 17 '16 at 13:44
  • Please refer this link: [http://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files](http://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files) – RSKMR Feb 17 '16 at 13:46
  • Possible duplicate of [In Node.js, how do I "include" functions from my other files?](https://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files) – Towerss Aug 28 '17 at 23:17

4 Answers4

9

In this file you add

module.exports = Fitness

Then where you want to use it you do

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

akc42
  • 4,447
  • 5
  • 36
  • 54
  • Am getting error(has no method 'get'), because get method is inside of this.users = {dataSources: { get: function(params, callback) { }}}. Is there any fix for this – VG__ Feb 17 '16 at 13:55
3

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 
Piyush Sagar
  • 2,241
  • 19
  • 26
  • I tried in another way: (module.exports = google) this google contain all the API's of google, including fitness API as above nodejs. while accessing the google from another file, it returns o/p as "fitness:[Function]" . So in this case, how its possible to access the fitness get method from google. Is it possible or not. If I tried like this 'google.fitness' it return 'Function' and in case of 'google.fitness.users' it return 'undefined'. Could any one help me to know the solution for this. (example calls google, google calls fitness, fitness have the get method) – VG__ Feb 18 '16 at 04:52
2

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?

PostCrafter
  • 655
  • 5
  • 15
IceFire
  • 3,719
  • 2
  • 19
  • 46
0
    * just make 2 file one will have file read operation and another will fetch data
 Hi like you are having a file abc.txt and many more 
    create 2 file   ||  fileread.js and  fetchingfile.js
    **in fileread.js  write this code***




         function fileread(filename){

            var contents= fs.readFileSync(filename);
            return contents;
            }

            var fs =require("fs");  // file system

            //var data= fileread("abc.txt");
            module.exports.fileread =fileread;
            //data.say();
            //console.log(data.toString());


        **in fetchingfile.js  write this code**

            function myerror(){

                console.log("Hey need some help");
                console.log("type file=abc.txt");
            }

            var ags = require("minimist")(process.argv.slice(2),{string: "file" });
            if(ags.help || !ags.file) {

                myerror();
                process.exit(1);
            }
            var hello=require("./fileread.js");
            var data= hello.fileread(ags.file);  // importing module here 
            console.log(data.toString());

    **in your cmd type 
    node fetchingfile.js --file=abc.txt** 



    you are passing file name as a argument moreover include all file in readfile.js instant of passing it 
    thanks 
Gajender Singh
  • 963
  • 9
  • 12