Reputation: 4360
I'm building a sample app based on this sequelize example.
I'm getting the following error:
TypeError: models.User.findAll.then is not a function
config/config.json
{
"development": {
"username": "postgres",
"password": "root",
"database": "test",
"host": "127.0.0.1",
"dialect": "postgres",
"port": "5432"
}
...same for test & production
controllers/user.js
var express = require('express');
var router = express.Router();
var models = require('../models');
router.get('api/user', function (req, res) {
models.User.findAll.then(function(users) {
res.json(users);
});
});
models/User.js
"use strict";
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
id : {
type : DataTypes.INTEGER,
primaryKey: true,
allowNull : false,
},
name : {
type : DataTypes.STRING,
allowNull : false,
},
email : {
type : DataTypes.STRING,
allowNull : false,
},
role : {
type : DataTypes.INTEGER,
allowNull : false,
}
}, {
tableName: 'user'
});
return User;
};
models/index.js is essentially the same as in the github repo
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/../config/config.json')[env];
var sequelize = new Sequelize(config.database, config.username, config.password, config);
var db = {};
fs.readdirSync(__dirname).filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
}).forEach(function(file) {
var model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Upvotes: 2
Views: 4502
Reputation: 4037
findAll
is a method.
You need to call it, findAll()
.
This would return a promise which can be chained using then
Upvotes: 4