当前位置: 首页 > 知识库问答 >
问题:

使用Sequalize获取“TypeError:无法读取未定义的属性'findAll'”

瞿博学
2023-03-14

我正在使用PostgreSQL、Sequelize和Express开发一个简单的CRUD应用程序。并以本教程作为参考。我的模型或数据库连接似乎有问题。详情如下:

文件夹结构:
server
|__config
|__db.js
|__env.js
|__models
|__missions.js
|__router
|__routes
|__missions.js
|__index.js
|__index.js

包裹json

server/config/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;

server/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;

模型/任务。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;
};

路由器/路由/任务。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);
      });
  });

} 

路由器/索引。js

'use strict'

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



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

索引。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
);

邮递员出错
[GET]http://localhost:4000/missions

类型错误:无法读取属性'findAll'的未定义在 /home/gx/Documents/projects/OSCTest1/server/router/routes/missions.js: 5:16在Layer.handle[作为handle_request](/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/layer.js:95: 5)在下一个(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/route.js:137: 13)在Route.dispatch(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/route.js:112: 3)在Layer.handle[ashandle_request](/home/gx/Documents/project/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/project/OSCTest1/node_modules/Express/lib/router/index.js:335: 12)at Next(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/index.js:275: 10)at /home/gx/Documents/projects/OSCTest1/index.js:19: 3 at Layer.handle[作为handle_request](/首页/gx/文档/项目/OSCTest1/node_modules/快递/库/路由器/layer.js:95: 5)在trim_prefix(/home/gx/文档/项目/OSCTest1/node_modules/快递/库/路由器/index.js:317: 13)在 /home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/index.js:284: 7在Function.process_params(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/index.js:335: 12)在jsonParser(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/index.js:275: 10)/node_modules/body-parser/lib/types/json.js: 110:7)在trim_prefix(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/index. js: 95:5)处理[为handle_request](/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/index. js: 317:13)在 /home/gx/Documents/projects/OSCTest1/node_modulesprocess_params(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/index. js: 335:12)在下一个(/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/index. js: 275:10)在logger(/home/gx/Documents/project/OSCTest1/node_modules/morgan/index. js: 144:5)处理[作为handle_request](/home/gx/Documents/project/OSCTest1/node_modules/Express/lib/router/layer. js: 95:5)

共有1个答案

公孙弘深
2023-03-14

试着跟随

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

在路由器/路由/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);
              });
          });

        } 
 类似资料: