使用Sequlize

时间:2019-06-07 03:07:01

标签: javascript node.js postgresql express sequelize.js

我正在使用PostgreSQL,Sequelize和Express开发一个简单的CRUD应用程序。 然后跟随this tutorial as a reference。我的模型或数据库连接似乎有问题。请找到以下详细信息:

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

package.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;

服务器/配置/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);
      });
  });

} 

router / 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
);

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

TypeError:无法读取未定义的属性'findAll'     在/home/gx/Documents/projects/OSCTest1/server/router/routes/missions.js:5:16     在Layer.handle [作为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 [作为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 [作为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 [作为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 [作为handle_request](/home/gx/Documents/projects/OSCTest1/node_modules/express/lib/router/layer.js:95:5)

1 个答案:

答案 0 :(得分:0)

尝试关注

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);
              });
          });

        }