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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35458247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:56:00  来源:igfitidea点击:

How to call the function from other node js file

node.jsgoogle-fitgoogle-api-nodejs-client

提问by VG__

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.

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

Can any one tell me how to call the get method from this file to another node js file

谁能告诉我如何从这个文件调用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);
      }     }   }; }

回答by akc42

In this file you add

在这个文件中你添加

module.exports = Fitness

module.exports = Fitness

Then where you want to use it you do

然后你想在哪里使用它

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

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

回答by Piyush Sagar

Very first thing I noted is 'dataSources' property is inside 'users' object.So you need to do users.dataSourcesfrom outside to access this object.

我注意到的第一件事是 'dataSources' 属性在 'users' 对象内。所以你需要从外部执行users.dataSources来访问这个对象。

To make things work.

使事情发挥作用。

I have made some changes in fitness.js

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

现在编写以下代码以访问另一个文件中的 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 

回答by IceFire

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

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

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?

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

回答by Gajender Singh

    * 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