Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1126 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: Cannot read property 'findAll' of undefined (expressjs)

#1
> 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;
};

Controller:


models = require('./../models');

exports.index = function (request, response, next) {
models.User.findAll({attributes: ['id', 'username']});
};
Reply

#2
You have created two instances of sequelize. One in models/index.js in line 12/14 and second instance in server script in line 19. And you start second instance, but in model you tried to use first instance.

Your model/index.js file is ok.
In your server file add

var database = require('path/to/models');

change your db start to:

```
database.sequelize
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
}, function (err) {
console.log('Unable to connect to the database:', err);
});
```

And you have to pass 'database' object to your controllers instead of `models = require('./../models');` and from your controllers you have access to your model : `database.User`
Reply

#3
Your `model/index.js` looks fine. In your controller try `findAll()` method inside `sequelize.sync().then(function () {`

Here is my approach to the problem

> nb: instead of `models/index.js` i have a similar `db.js` file inside
> config folder which having the dbconnection scripts and sequelize object.

My **userController.js** will look like (working code) :

var db = require('./../config/db'),
seq = db.seq,
Sequelize = db.Sequelize;

module.exports = function(app) {
app.get('/getUsers',function(req,res){
var Users = require('../models/UserModel')(app); //since i am binding to a single object - app
seq.sync().then(function () {
Users.findAll({
attributes: ['usr_code', 'usr_name']
}).then(function (users) {
users.forEach(function(user,index,arr){
console.log(user.usr_code);
});
});
});
});

}

Hope this helps you. :)
Reply

#4
I had the same issue, and the changes below worked for me. This might be useful for future users -

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.
Reply

#5
I also had the same issue, you need to check your table name that's it.
Reply

#6
you may have this issue when relationships or associations are not defined with db connection.
Reply

#7
This error is because you intend to use Sequelise in the controller before fully connecting it to the database

try use .then or async await
Reply

#8
I just had the same problem, a few minutes ago.

This is how I imported the models:


const {product: Product, cartItem: CartItem} = require('../models');

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through