如何使用node.js从一个模块访问JSON对象?

时间:2021-12-19 06:42:14

This is my module1:

这是我的module1:

var fs = require('fs');
var obj;
    exports.module1= function(ret)
    {
    fs.readFile('source.json', 'utf8', function (err, data)
    {
        if (err) {
       return console.error(err);
        }
    obj=JSON.parse(data);
    console.log(obj);

    return obj;

});
}

Module2:

var module1 = require('./module1.js');
var obj=module1.module1();

var callback = function () {
console.log(obj);
};
setTimeout(callback, 10000);

The obj of module2 is not getting updated with returned value of module1. I am newbie btw.

module2的obj没有使用module1的返回值进行更新。我是新手顺便说一下。

2 个解决方案

#1


0  

I believe ur problem is that fs.readFile is an async call and its return value will not be passed to the obj defined in Module2.

我相信你的问题是fs.readFile是一个异步调用,它的返回值不会传递给Module2中定义的obj。

Just for reference u may pass an callback function to module1's export and then call when the file reading completes.

仅供参考,您可以将回调函数传递给module1的导出,然后在文件读取完成时调用。

Module1:

var fs = require('fs');
var obj;
exports.module1= function(callback)
{
    fs.readFile('source.json', 'utf8', function (err, data)
    {
        if (err) {
            return console.error(err);
        }
        obj=JSON.parse(data);
        console.log(obj);

        callback(obj)

    });
}

Module2:

var module1 = require('./module1.js');
var obj;
module1.module1(function(result){
    obj = result;
});

var callback = function () {
    console.log(obj);
};
setTimeout(callback, 10000);

#2


0  

You can share the object by passing it the the global context, which means the object will be usable from any file at any time, but the main JavaScript code (which requires the module) will be able to access it too. If you don't want it to access the code, comment this post and I'll make a script to do that.

您可以通过将对象传递给全局上下文来共享该对象,这意味着该对象可以随时从任何文件中使用,但主JavaScript代码(需要该模块)也能够访问它。如果您不希望它访问代码,请评论此帖子,我将制作一个脚本来执行此操作。

The most simple way is to share the object :

最简单的方法是共享对象:

global.__module_object = obj;

And anywhere you'll be able to access the object by doing global.__module_object.data = "Hello world !"; for example.

通过执行global.__module_object.data =“Hello world!”,您可以在任何地方访问该对象。例如。

#1


0  

I believe ur problem is that fs.readFile is an async call and its return value will not be passed to the obj defined in Module2.

我相信你的问题是fs.readFile是一个异步调用,它的返回值不会传递给Module2中定义的obj。

Just for reference u may pass an callback function to module1's export and then call when the file reading completes.

仅供参考,您可以将回调函数传递给module1的导出,然后在文件读取完成时调用。

Module1:

var fs = require('fs');
var obj;
exports.module1= function(callback)
{
    fs.readFile('source.json', 'utf8', function (err, data)
    {
        if (err) {
            return console.error(err);
        }
        obj=JSON.parse(data);
        console.log(obj);

        callback(obj)

    });
}

Module2:

var module1 = require('./module1.js');
var obj;
module1.module1(function(result){
    obj = result;
});

var callback = function () {
    console.log(obj);
};
setTimeout(callback, 10000);

#2


0  

You can share the object by passing it the the global context, which means the object will be usable from any file at any time, but the main JavaScript code (which requires the module) will be able to access it too. If you don't want it to access the code, comment this post and I'll make a script to do that.

您可以通过将对象传递给全局上下文来共享该对象,这意味着该对象可以随时从任何文件中使用,但主JavaScript代码(需要该模块)也能够访问它。如果您不希望它访问代码,请评论此帖子,我将制作一个脚本来执行此操作。

The most simple way is to share the object :

最简单的方法是共享对象:

global.__module_object = obj;

And anywhere you'll be able to access the object by doing global.__module_object.data = "Hello world !"; for example.

通过执行global.__module_object.data =“Hello world!”,您可以在任何地方访问该对象。例如。