Node.js 带有express、can和x27的节点;t负荷模型

Node.js 带有express、can和x27的节点;t负荷模型,node.js,express,loading,Node.js,Express,Loading,我将NodeJS与express、commit、body解析器等一起使用 我试图在var中加载一个模块,这样我就可以使用它们的函数,但我得到了一个: TypeError:无法读取未定义的属性“listar” 在app.get(/home/sartori/Documentos/Projetos/react system/api/controllers/autores.js:6:11) 在Layer.handle[作为handle_请求](/home/sartori/Documentos/Proj

我将NodeJS与express、commit、body解析器等一起使用

我试图在var中加载一个模块,这样我就可以使用它们的函数,但我得到了一个:

TypeError:无法读取未定义的属性“listar”
在app.get(/home/sartori/Documentos/Projetos/react system/api/controllers/autores.js:6:11)
在Layer.handle[作为handle_请求](/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/Layer.js:95:5)
下一步(/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/route.js:137:13)
在Route.dispatch(/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/Route.js:112:3)
在Layer.handle[作为handle_请求](/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/Layer.js:95:5)
at/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/index.js:281:22
在Function.process_参数(/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/index.js:335:12)
下一步(/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/index.js:275:10)
at/home/sartori/Documentos/Projetos/react system/api/node_modules/express validator/lib/express_validator.js:250:5
在Layer.handle[作为handle_请求](/home/sartori/Documentos/Projetos/react system/api/node_modules/express/lib/router/Layer.js:95:5)

这是我的控制器:

module.exports = app => {
app.get('/api/autores', (req,res) => {
    let connection = app.connection.connectionFactory();
    let autores = app.models.AutoresDao(connection);

    autores.listar((error,result) => {
        if(error) {
            console.log('Erro ao listar os autores: ' + error);
            res.status(500).send(error);
        } else {
            res.json(result);
        }
    });

});
}

这是AutoresDao:

function AutoresDao(connection) {
    this._connection = connection;
}

AutoresDao.prototype.listar = callback => this._connection.query('SELECT * FROM autores', callback);

module.exports = function() {
    return AutoresDao;
}
问题是:无法读取未定义的属性“listar”

显然,问题在于加载AutoresDao文件

我100%确定文件路径,AutoresDao在models文件夹中,已经加载到委托中。控制器、连接、快速配置等也一样


知道我做错了什么吗?

如果要以那种方式格式化对象构造函数,则需要使用“new”关键字。您必须重新格式化AutoresDao函数以显式返回对象,或者在控制器中将其分配给autores时使用“new”关键字

请查看本文以了解不同实例化模式的解释


希望有帮助!干杯。

是的,这解决了我的问题。谢谢你的链接,非常有用!