TypeError:无法读取属性'findAll'的未定义
TypeError: Cannot read property 'findAll' of undefined
我正在开发一个使用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();
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- Jquery未定义函数正在停止其他操作
- AngularJS指令出错-无法读取属性'编译'的未定义
- 这.SOMETHING 总是返回未定义的 - extjs
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- TypeError:无法读取属性'findAll'的未定义
- ember dev fixes错误:“;断言失败:findAll的响应必须是数组,而不是未定义的“”;