NodeJS sequelize and sqlite error: Cannot read property 'findAll' of undefined


NodeJS sequelize and sqlite error: Cannot read property 'findAll' of undefined



I'm in the process of learning how to use NodeJS using express it's minimalistic web framework. I've also added sequelize an ORM and sqlite.
The project folder name is expressapp so in the root directory I've created a models file using sqlite


expressapp



models.js


const Sequelize = require("sequelize");

const db = new Sequelize({
dialect: "sqlite",
storage: "./db.sqlite"
});

const User = db.define("user", {
username: { type: Sequelize.STRING },
loggedIn: { type: Sequelize.BOOLEAN }
});

const Messages = db.define("messages", {
public: { type: Sequelize.BOOLEAN },
to: { type: Sequelize.STRING },
messsage: { type: Sequelize.BOOLEAN },
createdBy: { type: Sequelize.STRING },
createdAt: { type: Sequelize.DATE }
})

db.sync();

module.exports = db;



inside it's route file routesindex.js I've imported the model and try to check if a username exists (for now with a static text) before deciding if I should add the username to the list.


routesindex.js



index.js


var express = require('express');
const models = require("./../models");

var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});

router.post('/login-ajax', function (req, res) {
//assume the front end ensured there's valid post data
let existing = models.user.findAll({
where: {
username: 'ME'
}
});

res.send('Post data received')
})

module.exports = router;



On loading this is the message on the terminal from nodejs which seems to indicate things are working ok.


[nodemon] restarting due to changes...
[nodemon] starting `node ./bin/www expressapp`
Tue, 03 Jul 2018 06:03:33 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modulessequelizelibsequelize.js:242:13
Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(255), `loggedIn` TINYINT(1), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`users`)
Executing (default): CREATE TABLE IF NOT EXISTS `messages` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `public` TINYINT(1), `to` VARCHAR(255), `messsage` TINYINT(1), `createdBy` VARCHAR(255), `createdAt` DATETIME, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`messages`)



I then on the console from the browser check to see if there's a bug with a post request using


$.post("/login-ajax", function( data ) {
console.log(data);
});



Then I get back the error Cannot read property 'findAll' of undefined and a 500 response error.


Cannot read property 'findAll' of undefined




2 Answers
2



It can't find the "model.user" object. Are you sure that is existing? It's only a hunch but you can check that.





According to another stackoverflow post I found they suggest you should use the name used in the db.define("user" not the const name User anyway both crash the same
– Samuel M.
Jul 3 at 6:33


db.define("user"


User



Are you sure you imported your model correctly ?



If your project structure looks like this:


root
- models.js
- routes
- - index.js



And you want to import models.js inside index.js, then you need to use:


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



( or at least try because you will find different approaches around the web when it comes to importing local modules )



Or you just use quick and dirty console.log(models); to see if it got imported correcty and to see if it is defined and not undefined


const models = require('./../models');
console.log(models);





don't think the import is the issue, I started with the require('../models'); anyway here's a gist of using the first method gist.github.com/Six-wars/6a9e5a3ad7fea78b3de6dc956a9aa56a and the second method gist.github.com/Six-wars/93e310dd6c31d3297b4f2b6c3c558164
– Samuel M.
Jul 3 at 7:53


require('../models');





Have you tried logging the complete models Object to the console for troubleshooting ? E.g. with console.dir(models,{depth:null});
– Pascal Lamers
Jul 3 at 8:03





it's the same thing, some jargon of text and a final result of undefined
– Samuel M.
Jul 3 at 8:13


undefined





Then A it's not imported correctly or B the creation of const db is somehow wrong. Try logging db inside models.js and see everything gets created the way it should.
– Pascal Lamers
Jul 3 at 9:15






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?