TypeError:无法读取未定义的属性'findAll'(express.js) [英] TypeError: Cannot read property 'findAll' of undefined (expressjs)

查看:331
本文介绍了TypeError:无法读取未定义的属性'findAll'(express.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TypeError:无法读取未定义(expressjs)的属性'findAll'.

TypeError: Cannot read property 'findAll' of undefined (expressjs).

所有功能(连续化)均不起作用.所有错误:无法读取属性"sequelize方法" ...

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

用户"是一个变量,已将序列化定义分配给该变量,并且该变量在该模型定义之外未被识别. 用户"是要创建的表名以及序列化模型类名,该名称需要在任何类型的数据库查询中使用.

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'(express.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆