简体   繁体   中英

TypeError: Cannot read property 'findAll' of undefined

I am developing a web app that uses angularjs, nodejs, and postgresql. I have two tables in my database, one for inventory and the other for client. I combined my index.js files, in my routes folder, in which both tables have a findAll function. When I put the two references for the tables in the index.js, only one works at a time. Here is the template that both tables use that includes findAll:

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

Has anyone done what I am trying to do? UPDATE: Here is entire index.js file :

/*
* 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' });
});
};

You are doing your exports incorrectly. Here is an example of how to do it correctly.

Lets say I want to export 2 functions for a module this is a simple way to achieve it.

myModule.js

module.exports = {
    awesomeMethod: function () {

    },
    coolMethod: function () {

    }
}

Now to use it I would do the following

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM