TypeError: Cannot read property #39;findAll#39; of undefined (expressjs)(TypeError:无法读取未定义的属性“findAll(expressjs))
问题描述
TypeError:无法读取未定义(expressjs)的属性findAll".
TypeError: Cannot read property 'findAll' of undefined (expressjs).
所有功能(续集)都不起作用.所有错误:无法读取属性续集方法"...
All functions (sequelize) are not working. All errors: Cannot read property 'sequelize method' ...
module.exports = function (sequelize, DataTypes) {
var User = sequelize.define('user', {
email: {type: DataTypes.STRING(32), unique: true, allowNull: false},
});
return User;
};
控制器:
models = require('./../models');
exports.index = function (request, response, next) {
models.User.findAll({attributes: ['id', 'username']});
};
推荐答案
我遇到了同样的问题,下面的更改对我有用.这可能对未来的用户有用 -
I had the same issue, and the changes below worked for me. This might be useful for future users -
当你使用sequelize模型时,你需要使用定义好的模型类名,在findAll行中是user"而不是User":
when you use the sequelize model you need to use the defined model class name, which is "user" instead of "User" in line findAll:
models = require('./../models');
exports.index = function (request, response, next) {
models.user.findAll({attributes: ['id', 'username']});
};
用户"是分配了续集定义的变量,并且在该模型定义之外无法识别.用户"是正在创建的表名以及 sequelize 模型类名,这需要在任何类型的数据库查询中使用.
The "User" is a variable to which the sequelize definition is assigned and its not recognized outside that model definition. The "user" is the table name that is getting created as well as the sequelize model class name, and this needs to be used in any type of db query.
这篇关于TypeError:无法读取未定义的属性“findAll"(expressjs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError:无法读取未定义的属性“findAll"(expressjs)


基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01