我正在使用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)
试着跟随
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);
});
});
}
我是一个初学者,学习反应和还原。我写了这个关于如何在Redux中使用Connect.js的演示。搜索这类问题,但我的代码没有正确的答案。我得到了一个未定义的上下文。是错字吗?还是我以错误的方式传递了上下文?提前谢了。这是我的代码。 index.js /store/index.js Constants.js
如果函数在组件中,那么一切都很好。如果我把它单独取出并导出到一个组件中,那么我会得到一个错误TypeError:不能读取未定义的属性(读取'reduce')
我有一个简单的代码,但我收到(有时)错误的结果,这是因为nodejsasync,我知道。最后,我需要更新“this.setState({active:options})”。因此,为了解决NodeJS的异步问题,我使用了require(“异步”)库。但是现在我的简单代码变得相当复杂,结果也不是100%好。 同样在“函数道路”上,当我使用“this.setState({actove:options})
TypeError:无法读取未定义的属性(读取“to isoString”)
问题内容: 我试图在ajax回调从REST api接收数据后设置组件的setState。这是我的组件构造函数代码 然后,我有一个如下所示的方法。 现在,这是我执行getAJAX请求的getPosts函数。 我想设置状态,但出现以下错误。 不太清楚是什么原因造成的。如果有人指出我正确的方向,那将真的很有帮助。提前致谢。 问题答案: 还绑定回调函数,以便回调内部指向React Component的上下
我试图使用node.js express web服务器中的@tensorflow/tfjs-node模块。但是,我得到了下面的错误。我不明白为什么我会出现这个错误。我刚刚在node.js服务器中添加了1行代码。我使用“npm install@tensorflow/tfjs-node”完成的安装。可能的问题是什么? null 先谢谢你, var nonMaxSuppressionV3Impl=tf.