【问题标题】:How to call the function from other node js file如何从其他节点js文件调用函数
【发布时间】:2016-02-17 13:37:07
【问题描述】:

这些代码存储在单独的文件中,我试图从这个文件调用 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);
      }     }   }; }

【问题讨论】:

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


【解决方案1】:

在这个文件中添加

module.exports = Fitness

然后你想在哪里使用它

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

【讨论】:

  • 我得到错误(没有方法'get'),因为get方法在this.users = {dataSources:{get:function(params,callback){}}}中。有什么解决办法吗
【解决方案2】:

我首先注意到的是 'dataSources' 属性位于 'users' 对象内。因此您需要从外部执行 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

现在编写下面的代码来访问另一个文件中的健身模块

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 

【讨论】:

  • 我尝试了另一种方式:(module.exports = google)这个google包含google的所有API,包括上面nodejs的fitness API。从另一个文件访问 google 时,它​​返回 o/p 作为 "fitness:[Function]" 。所以在这种情况下,如何从谷歌访问健身获取方法。有没有可能。如果我尝试这样的“google.fitness”,它会返回“功能”,如果是“google.fitness.users”,它会返回“未定义”。任何人都可以帮助我了解解决方案。 (例子调用google,google调用fitness,fitness有get方法)
【解决方案3】:

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(...);

你试过吗?如果是,您究竟在哪里得到错误?

【讨论】:

  • 完美,谢谢 - 今天晚上第一次学习节点,这很有帮助
【解决方案4】:
    * 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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多