gpt4 book ai didi

javascript - 使用 Sequelize 获取 "TypeError: Cannot read property ' findAll' of undefined"

转载 作者:行者123 更新时间:2023-11-29 12:23:26 27 4
gpt4 key购买 nike

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

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

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

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

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

Postman 错误
[获取] http://localhost:4000/missions

类型错误:无法读取未定义的属性“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)

最佳答案

尝试跟随

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

}

关于javascript - 使用 Sequelize 获取 "TypeError: Cannot read property ' findAll' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56487477/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com