(节点js)-TypeError:无法调用未定义的方法'getData' [英] (Node js) - TypeError: Cannot call method 'getData' of undefined

查看:97
本文介绍了(节点js)-TypeError:无法调用未定义的方法'getData'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经走了好几天,但实际上我觉得我在这里缺少了一部分概念... node.js我还很陌生,我正试图称其为从我的主类中的不同模块讲解方法...

I've been looking around for a few days not and really I feel like I'm missing a part of the concept here... I'm fairly new with node.js, and I'm trying to call a method from a different module in my main class per-say...

这里是代码.

inputReader.js

inputReader.js

    (function() {
    var dir = './views/';   // Declare the directory to be scanned
    var data = {}   // Create array object for storage 

    fs.readdir(dir, function(err, files) {
        if (err) {
            throw err;
        }
        var c = 0;  // Declare a var c = 0; initial condition of a for loop
        files.forEach(function(file) {
            c++;    // Increment a counter in the for-loop condition
            fs.readFile(dir+file, 'utf-8', function(err, string) {
                if (err) {
                    throw err;
                }
                if ( 0 === -3) {
                    data[file] = string;    // Throws into data object string of contents within the file being read
                    console.log(data);  // We only need this to test using console (the contents being stored)
                }
            });
        });
    }); 
    module.exports.getData = function() {
        return data();
    }
}());

这就是我试图在app.js中调用它的方式

And here is how I'm trying to call it in app.js

    var inputReader = require('./inputReader').inputReader;

app.get('/', function(req, res){
  res.send(inputReader.getData());
});

app.listen(3000);
console.log('Listening on port 3000');

我的预测是,如果我正确执行了此操作,则本地主机页面将显示我指定要读取的应用程序文件夹中文件的内容; ./views/..但很明显,我做错了什么,因为我得到的错误是:

My prediction for if I did this correctly, my localhost page would display the contents of the files within the folder I designated the app to read; ./views/.. but clearly, I am doing something very wrong as the error I'm getting is:

TypeError:无法在回调(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \通过时(c:\ Users \在Router._dispatch(c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ router \ index.js中的Brian \ documents \ visualizer \ node_modules \ express \ lib \ router \ index.js:145:5): 173:5)在下一个(c:\ Users \ Brian \ documents \ visualizer \在Object.expressInit处的node_modules \ express \ node_modules \ connect \ lib \ proto.js:193:15 [作为句柄](c:\ Users \ Brian \ documents \ visualizer \ node_modules \ express \ lib \ middleware.js:30: 5)在Object.query [作为句柄](c:\ Users \ Brian \文件\可视化器\ n ode_modules \ express \ node_modules \ connect \ lib \ middleware \ query.js:44:5)

TypeError: Cannot call method 'getData' of undefined at c:\Users\Brian\documents\visualizer\app.js:21:24 at callbacks (c:\Users\Brian\documents\visualizer\node_modules\express\lib\router\index.js:164:37) at param (c:\Users\Brian\documents\visualizer\node_modules\express\lib\router\index.js:138:11) at pass (c:\Users\Brian\documents\visualizer\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (c:\Users\Brian\documents\visualizer\node_modules\express\lib\router\index.js:173:5) at Object.router (c:\Users\Brian\documents\visualizer\node_modules\express\lib\router\index.js:33:10) at next (c:\Users\Brian\documents\visualizer\node_modules\express\node_modules\connect\lib\proto.js:193:15) at Object.expressInit [as handle] (c:\Users\Brian\documents\visualizer\node_modules\express\lib\middleware.js:30:5) at next (c:\Users\Brian\documents\visualizer\node_modules\express\node_modules\connect\lib\proto.js:193:15) at Object.query [as handle] (c:\Users\Brian\documents\visualizer\node_modules\express\node_modules\connect\lib\middleware\query.js:44:5)

如果有人可以指引我正确的方向或向我解释我做错了什么,那将不胜感激

If someone may point me to the right direction or explain to me what I am doing wrong, that would be appreciated

谢谢! (对不起,谢谢您的长期阅读.)

Thank you! (And sorry for the long read..)

推荐答案

几种不同的书写方式:

// inputReader.js
module.exports.getData = function() {
    return data();
}

// app.js
var inputReader = require('./inputReader'); // inputReader contains getData
inputReader.getData();

// inputReader.js
module.exports.getData = function() {
    return data();
}

// app.js
var inputReader = require('./inputReader').getData; // inputReader is now getData
inputReader();

// inputReader.js
var theModule = {
    getData : function() {
        return data();
    }
}

module.exports = theModule;

// app.js
var inputReader = require('./inputReader');
inputReader.getData();

// inputReader.js
var theModule = function() { /* constructor */ };

theModule.prototype.getData = function() {
    return data();
};

module.exports = theModule;

// app.js
var inputReader = require('./inputReader');
new inputReader().getData();

这篇关于(节点js)-TypeError:无法调用未定义的方法'getData'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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