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

时间:2016-02-17 13:37:07

标签: node.js google-fit google-api-nodejs-client

这些代码存储在单独的文件中,我试图将get方法从此文件调用到另一个nodejs,但我只将[Function]作为输出。

任何人都可以告诉我如何从这个文件调用get方法到另一个节点js文件

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

4 个答案:

答案 0 :(得分:5)

在此文件中添加

module.exports = Fitness

然后你要使用它的地方

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

答案 1 :(得分:3)

我首先注意到的是' dataSources'物业在用户内部'因此,您需要从外部执行 users.dataSources 才能访问此对象。

使事情有效。

我在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

现在编写以下代码以访问另一个文件中的Fitness模块​​

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 

答案 2 :(得分:2)

get是一个内部函数,需要创建类。在您的模块中,您可以导出整个类:

module.exports.Fitness = Fitness;

在您的其他模块中:

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(...);
你尝试过吗?如果是的话,你究竟在哪里收到错误?

答案 3 :(得分: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