使用 Sequelize 获取“TypeError:无法读取未定义的属性‘findAll’”

Posted

技术标签:

【中文标题】使用 Sequelize 获取“TypeError:无法读取未定义的属性‘findAll’”【英文标题】:Getting "TypeError: Cannot read property 'findAll' of undefined" with Sequlize 【发布时间】:2019-10-22 13:22:21 【问题描述】:

我正在使用 PostgreSQL、Sequelize 和 Express 开发一个简单的 CRUD 应用程序。 并关注this tutorial as a reference。我的模型或数据库连接似乎存在问题。请查看以下详细信息:

文件夹结构: 服务器 |__配置 |__db.js |__env.js |__模型 |__missions.js |__路由器 |__路线 |__missions.js |__index.js |__index.js

package.json

服务器/配置/db.js

'use strict'

const Sequelize = require('sequelize');
const env = require('./env');
const sequelize = new Sequelize(env.DATABASE_NAME,             
env.DATABASE_USERNAME, env.DATABASE_PASSWORD, 
  host: env.DATABASE_HOST,
  port: env.DATABASE_PORT,
  dialect: env.DATABASE_DIALECT,
  define: 
    underscored: true
  
);


const db = ;

db.Sequelize = Sequelize;
db.sequelize = sequelize;


db.missions = require('../models/missions.js')(sequelize, Sequelize);


module.exports = db;

服务器/config/env.js

'use strict';

const env = 
  PORT: process.env.PORT || 4000,
  DATABASE_URL: process.env.DATABASE_URL || 
'postgres://user001:YourPassword@YourHost:5432/oscdbtest1',
  DATABASE_NAME: process.env.DATABASE_NAME || 'oscdbtest1',
  DATABASE_HOST: process.env.DATABASE_HOST || 'localhost',
  DATABASE_USERNAME: process.env.DATABASE_USERNAME || 'user001',
  DATABASE_PASSWORD: process.env.DATABASE_PASSWORD || 'password001',
  DATABASE_PORT: process.env.DATABASE_PORT || 5432,
  DATABASE_DIALECT: process.env.DATABASE_DIALECT || 'postgres',

  NODE_ENV: process.env.NODE_ENV || 'development',
;

module.exports = env;

models/missions.js

'use strict'

module.exports = (sequelize, DataTypes) =>   
  const Mission = sequelize.define('missions', 
    id: 
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4
    ,
    mission_name: 
      type: DataTypes.STRING,
      required: true
    ,
    launchvechicle_name: 
      type: DataTypes.STRING,
      required: true
    ,
    status: 
      type: DataTypes.STRING,
      required: true
    ,
  );
  return Mission;
;

router/routes/missions.js

'use strict';

module.exports = (app, db) =>   
  app.get('/missions', (req, res) => 
    db.missions.findAll()
      .then(missions => 
        res.json(missions);
      );
  );

  app.get('/mission/:id', (req, res) => 
   const id = req.params.id;
     db.missions.find(
      where:  id: id
      )
      .then(mission => 
        res.json(missions);
      );
  );

 

路由器/index.js

'use strict'

const routes = [
  require('./routes/missions')
];



module.exports = function router(app, db) 
  return routes.forEach((route) => 
    route(app, db);
  );
;

index.js

'use strict';

const express = require('express'),
  bodyParser = require('body-parser'),
  morgan = require('morgan'),
  db = require('./server/config/db.js'),
  env = require('./server/config/env'),
  router = require('./server/router/index');

const app = express();
const PORT = env.PORT;

app.use(morgan('combined'));
app.use(bodyParser.json());

app.use((req, res, next) => 
  res.header('Content-Type', 'application/json');
  next();
);

router(app, db);

db.sequelize.sync().then(() => 
  app.listen(PORT, () => 
     console.log('Express listening on port:', PORT);
  );
);

package.json


  "name": "osctest1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": 
  "dev": "nodemon index.js"
  ,
  "author": "",
  "license": "ISC",
  "dependencies": 
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "morgan": "^1.9.1",
    "nodemon": "^1.19.1",
    "pg": "^7.11.0",
    "pg-hstore": "^2.3.3",
    "sequelize": "^5.8.7",
    "sequelize-cli": "^5.4.0"
  ,
  "devDependencies": 
    "nodemon": "^1.19.1"
  

用于创建表的 Postgres 查询:

CREATE TABLE missions(
   item_id serial PRIMARY KEY,
   mission_name VARCHAR (50) NOT NULL,
   launchvehicle_name VARCHAR (50) NOT NULL,
   status VARCHAR (50) NOT NULL
);

邮递员错误 [获取]http://localhost:4000/missions

TypeError:无法读取未定义的属性“findAll” 在 /home/gx/Documents/projects/OSCTest1/server/router/routes/missions.js:5:16 在 Layer.handle [as handle_request] (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/layer.js:95:5) 在下一个(/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/route.js:137:13) 在 Route.dispatch (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/route.js:112:3) 在 Layer.handle [as handle_request] (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/layer.js:95:5) 在 /home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:281:22 在 Function.process_params (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:335:12) 在下一个(/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:275:10) 在 /home/gx/Documents/projects/OSCTest1/index.js:19:3 在 Layer.handle [as handle_request] (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/layer.js:95:5) 在 trim_prefix (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:317:13) 在/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:284:7 在 Function.process_params (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:335:12) 在下一个(/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:275:10) 在 jsonParser (/home/gx/Documents/projects/OSCTest1/node_modules/body-parser/lib/types/json.js:110:7) 在 Layer.handle [as handle_request] (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/layer.js:95:5) 在 trim_prefix (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:317:13) 在/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:284:7 在 Function.process_params (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:335:12) 在下一个(/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:275:10) 在记录器(/home/gx/Documents/projects/OSCTest1/node_modules/morgan/index.js:144:5) 在 Layer.handle [as handle_request] (/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/layer.js:95:5)

【问题讨论】:

错误消息应该指向导致问题的代码行。 routes/missions.js: - 有笔误吗?如果没有,请尝试单步执行代码。 该错误与 db.missions 有关,这意味着它无法在 db 中找到“missions”表。在mission.js 中你定义它为Mission,这里你称之为missions。 在 db.js 文件中测试这一行:db.missions = require('../models/missions.js')(sequelize, Sequelize); 【参考方案1】:

尝试关注

db.missions = require('../models/missions.js')(sequelize, Sequelize)

在 router/routes/missions.js 中

const db = require('server/config/db.js');
const Mission = db.missions;  
module.exports = (app, db) =>   
          app.get('/missions', (req, res) => 
            Mission.findAll()
              .then(missions => 
                res.json(missions);
              );
          );

          app.get('/mission/:id', (req, res) => 
           const id = req.params.id;
             Missions.find(
              where:  id: id
              )
              .then(mission => 
                res.json(missions);
              );
          );

         

【讨论】:

以上是关于使用 Sequelize 获取“TypeError:无法读取未定义的属性‘findAll’”的主要内容,如果未能解决你的问题,请参考以下文章

类型错误:defineCall 不是函数。要求()失败

Sequelize 错误:在 Index.js 中未定义 defineCall

Sequelize 迁移失败,无法读取未定义的属性“toString”[关闭]

TypeError:usert.addItem 不是函数

如何使用 Sequelize 从 GEOMETRY('POINT') 获取纬度/经度坐标?

使用 Sequelize 仅从行和关联中获取值