findAll不是一个函数

时间:2016-10-12 10:34:29

标签: node.js postgresql express sequelize.js

我正在构建一个基于此sequelize example的示例应用。

我收到以下错误:

  

TypeError:models.User.findAll.then不是函数

配置/ config.json

{
  "development": {
    "username": "postgres",
    "password": "root",
    "database": "test",
    "host": "127.0.0.1",
    "dialect": "postgres",
    "port": "5432"
  }
...same for test & production

控制器/ 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);
    });
});

模型/ 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 与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;

1 个答案:

答案 0 :(得分:3)

findAll是一种方法。

您需要将其称为findAll()

这将返回一个可以使用then

链接的承诺