简体   繁体   English

将变量值从 index.js 访问到 nodejs 上的另一个 js 文件

[英]Accessing variable value from index.js to another js file on nodejs

I am new to nodejs and I have index.js file which is called when I start my nodejs express server using nodemon index command.我是 nodejs 的新手,我有index.js文件,当我使用 nodemon index 命令启动我的 nodejs express 服务器时会调用该文件。

In this index.js file I am connecting to google drive and getting jwtClient object when the connection is successful.在此index.js文件中,我连接到谷歌驱动器并在连接成功时获取jwtClient object。

I want to use this jwtClient object in product.js file to upload image to google drive.我想在product.js文件中使用这个jwtClient object 将图像上传到谷歌驱动器。

In index.js file once I have value in jwtClient variable I am doing module.exports = jwtClient;index.js文件中,一旦我在jwtClient变量中有值,我正在做module.exports = jwtClient; and I am accessing jwtClient in product.js as const jwtClient = require('../index');我正在访问product.js中的jwtClient作为const jwtClient = require('../index'); which is not working.这是行不通的。

What are the ways I can access jwtClient in product.js from index.js without creating new object and having the same object reference.我可以通过哪些方式从 index.js 访问 product.js 中的 jwtClient 而无需创建新的 object 并具有相同的 object 引用。

In index.js file:在 index.js 文件中:

// configure a JWT auth client
let jwtClient = new google.auth.JWT(
    privatekey.client_email,
    null,
    privatekey.private_key,
    ['https://www.googleapis.com/auth/drive']);
//authenticate request
jwtClient.authorize(function (err, tokens) {
    if (err) {
        console.log(err);
        return;
    } else {
        console.log("Successfully connected to gdrive!");
    }
});

module.exports = jwtClient;

In product.js file在 product.js 文件中

const jwtClient = require('../index');

The problem may be that when you examine your jwtClient variable, the jwtClient.authorize did not execute yet.问题可能是当您检查jwtClient变量时, jwtClient.authorize尚未执行。 Make sure that in product.js file you wait till it happen.确保在product.js文件中等待它发生。 The easyest (but not best) way to do that is最简单(但不是最好)的方法是

const jwtClient = require('../index/jwtClient');

setTimeout(function() {
  console.dir(jwtClient);
}, 1000);

The issue was that I was exporting literal instead of Object. So the code module.exports = jwtClient;问题是我导出的是文字而不是 Object。所以代码module.exports = jwtClient; exports literal but jwtClient is object so I used exports.jwtClient = jwtClient;出口文字但 jwtClient 是 object 所以我使用exports.jwtClient = jwtClient; in index.js file and then I access it in product.js file asindex.js文件中,然后我在product.js文件中访问它

const index = require('../index.js');
console.log(index.jwtClient);

reference https://www.tutorialsteacher.com/nodejs/nodejs-module-exports参考https://www.tutorialsteacher.com/nodejs/nodejs-module-exports

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将value变量从javascript文件夹中的普通js文件转换为nodejs中的route / index.js文件并进行表达? - How do i take the value variable from a normal js file in the javascript folder to the routes/index.js file in nodejs and express? node.js如何从index.js执行其他js文件? - nodejs how to execute other js file from index.js? 从 index.js 导出变量并将其导入另一个 class - react.js - exporting a variable from index.js and importing it into another class - react.js Express:从/models/index.js访问req.session - Express: Accessing req.session from /models/index.js 如何从NodeJS中的静态页面访问index.js - How to access index.js from static page in NodeJS 将 index.js 中的 req,res 传递给 Node 中的另一个 js 文件 - Pass req,res from index.js to another js file in Node 在Node.js模块中使用日志(或index.js中的其他对象) - Using log (or other object from index.js) in nodejs modules 如何从控制器中获取index.js中定义的变量? - How to get a variable defined in index.js from within a controller? 将变量从一个js传递到node.js中的另一个js文件 - Passing variable from one js to another js file in nodejs 如何在我的 index.js 文件中处理从现有页面到另一个页面的 301 重定向 - How show I handle a 301 redirect from an existing page to another page in my index.js file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM