module.exports 返回 undefined [英] module.exports is returning undefined

查看:101
本文介绍了module.exports 返回 undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前是 Node JS 的新手,今天我试图从文件 data.json读取数据.

I am currently new to Node JS, and today I was trying to read data from a file data.json.

这里是 JSON 文件:

Here is the JSON file:

{"username":"rahul_v7","password":"9673"} {"username":"7vik","password":"3248"} {"username":"pradypot_2","password":"6824"} {"username":"ad_1","password":"9284"} {"username":"premchand_4","password":"4346"} 

而且,我使用文件 GetData.js 中的以下代码来读取 data.json 中的数据:

And, I was using the below code present in a file GetData.js, to read the data present in the data.json:

'use strict';
const fs = require('fs');

let res = '', resObjs = [];
let fin = fs.createReadStream('F:/RahulVerma/NodeJS/data.json', 'utf-8');
fin.on('data', data => {
    if(data.length > 0) res += data;
}).on('end', () => {
    if(res.length > 0) {
        let resArr = res.trim().split(' ');

        for(let i = 0; i < resArr.length; i++) {
            resObjs.push(JSON.parse(resArr[i]));
        }

        module.exports.objects = resObjs;
    }
});

如您所见,我正在导出resObjs 数组(实际上是一个对象数组)到另一个名为AppendData.js,具体如下:

As you can see, I am exporting the resObjs array, which is actually an array of objects, to an another file named AppendData.js, which is given below:

'use strict';
const fs = require('fs');
const getObjs = require('./GetData');

console.log(getObjs.objects);

但是,当我在 Node.js 9.3.0 (ia32) 中运行 AppendData.js 时,它给出了以下输出:

But, when I run AppendData.js in Node.js 9.3.0 (ia32), it gives the following output:

推荐答案

出现问题是因为您试图在 GetData 上加载 AppendData.js 中的对象之前使用它们.js 由于 fs.createReadStream 是异步的.要解决这个问题,只需让 module.exports 成为一个期待 GetData.js 回调的函数,例如:

The problem happens because you're trying to use the objects in AppendData.js before they are loaded on GetData.js due to fs.createReadStream being asynchronous. To fix this just make module.exports be a function that expect a callback in GetData.js like:

'use strict';
const fs = require('fs');

module.exports = function(callback) {
    let res = '', resObjs = [];
    let fin = fs.createReadStream('F:/RahulVerma/NodeJS/data.json', 'utf-8');
    fin.on('data', data => {
        if(data.length > 0) res += data;
    }).on('end', () => {
        if(res.length > 0) {
            let resArr = res.trim().split(' ');

            for(let i = 0; i < resArr.length; i++) {
                resObjs.push(JSON.parse(resArr[i]));
            }

            callback(resObjs);                        // call the callback with the array of results
        }
    });
}

然后你可以像这样在 AppendData.js 中使用它:

Which you can then use like this in AppendData.js:

'use strict';
const fs = require('fs');
const getObjs = require('./GetData');                 // getObjs is now a function

getObjs(function(objects) {
    console.log(objects);
});

这篇关于module.exports 返回 undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆