TypeError:无法读取属性'findAll'的未定义

TypeError: Cannot read property 'findAll' of undefined

本文关键字:未定义 findAll 属性 读取 TypeError      更新时间:2023-09-26

我正在开发一个使用angularjs、nodejs和postgresql的web应用程序。我的数据库中有两个表,一个用于库存,另一个用于客户。我将我的index.js文件合并在routes文件夹中,其中两个表都有findAll函数。当我把表的两个引用放在index.js中时,一次只有一个有效。这是两个表使用的模板,其中包括findAll:

exports.getobjects = function(req, res) {
models.Object.findAll().then(function(objectss){
    res.json(objects);
});
};

有人做了我想做的事吗?更新:这是整个index.js文件:

/*
* GET home page.
*/
var models = require("../models");
exports.index = function(req, res) {
res.render('index', {
    title : 'title'
 });
 };
 /* clients */
 exports.getclients = function(req, res) {
 models.Client.findAll().then(function(clients){
    res.json(clients);
});
};
exports.saveclients = function(req, res) {
models.Client.create({
    name: req.body.name,
    ssn: req.body.ssn,
    dln: req.body.dln,
    dls: req.body.dls,
    zip: req.body.zip,
    email: req.body.email,
    notes: req.body.notes,     
}).then(function(clients){
    res.json(clients.dataValues);
}).catch(function(error){
    console.log("ops: " + error);
    res.status(500).json({ error: 'error' });
});
};
/*inventory*/
exports.getunits = function(req, res) {
models.Unit.findAll().then(function(units){
    res.json(units);
});
};
exports.saveunits = function(req, res) {
models.Unit.create({
    name: req.body.text,
    quant: reg.body.quant                        
}).then(function(units){
    res.json(units.dataValues);
}).catch(function(error){
    console.log("ops: " + error);
    res.status(500).json({ error: 'error' });
});
};

您的导出操作不正确。下面是一个如何正确操作的例子。

比方说,我想为一个模块导出2个函数,这是一种简单的实现方法

myModule.js

module.exports = {
    awesomeMethod: function () {
    },
    coolMethod: function () {
    }
}

现在要使用它,我会做以下

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

myModule.awesomeMethod();
myModule.coolMethod();