라우팅에 실패한 이유를 추적하는 데 문제가 있습니다. 나는 Sequelize and Express routing을 처음 사용합니다.Sequelize 및 Express로 API 라우팅에 오류가 발생했습니다.
목표 - API 끝점 '/ v1/agent/: id'에 액세스 할 때 Sequelize 쿼리에서 JSON 응답을 반환하고 싶습니다. 이미 쿼리가 작동하는지 확인하고 내 Agent
모델에 매핑 된 한 행을 가져 왔습니다.
앱을 실행하면 노드에서 Router.use() requires middleware function but got a ' + gettype(fn));
예외가 발생합니다. 예외 initializeDb 함수에서 오는 있지만 왜 그지 모르겠다./경로에서
import http from 'http';
import bodyParser from 'body-parser';
import express from 'express';
import sequelize from 'sequelize';
import config from './config';
import routes from './routes';
let app = express();
app.server = http.createServer(app);
app.use(bodyParser.json({
limit:config.bodyLimit
}));
app.use('/v1', routes);
app.server.listen(config.port);
console.log('API listening on port ' + app.server.address().port);
export default app;
내하는 index.js 파일 :
import express from 'express';
import config from '../config';
import initializeDb from '../db';
import agent from '../controller/agent'
// handle db configs
let router = express();
initializeDb(db => {
router.use('/agent', agent({config, db}));
});
export default router;
에이전트 모델에 대한 나의 컨트롤러 :
import sequelize from 'sequelize';
import { Router } from 'express';
import Agent from '../model/agent';
export default({config, db}) => {
let api = Router();
//query for the agent
api.post('/:id', (req, res) => {
sequelize
.query(
"SELECT agentnum AS agentno,fname,lname,agentname AS full_name,[status] FROM my_table WHERE agentnum='" + req.params.id + "'", {model:Agent})
.then(function(agent) {
console.log(agent);
res.json(agent);
});
});
}
그리고 마지막으로 여기
루트하는 index.js입니다 , 모델 agents.jsimport sequelize from '../db';
let agent = function(sequelize, DataTypes) {
sequelize.define('agent', {
agentnum: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false
},
fname : DataTypes.STRING,
lname : DataTypes.STRING,
fullname : DataTypes.STRING,
status : DataTypes.STRING
}, {
tableName: 'Base',
schema: 'Master',
freezeTableName: true,
timestamps: false
});
};
module.exports = agent;
누구든지이 두 번째 눈을 넣으시겠습니까?
오류가 다시 발생했습니다. 문제는 routes.js에서 미들웨어 호출과 관련이있는 것으로 보입니다. index.js에서 경로 미들웨어를 사용하는 방법을 살펴보십시오. 솔루션을 계속 사용하고 있지만 이것이 도움이 될까요? –
나는 그것이 당신이 설정에서 함수를 excecute 할 때 그것이 정의되지 않은 것을 반환한다는 사실과 관련이 있다고 믿는다. 그리고 그것은 체인을 끊는다. 단지 생각한다. –
Youre on something. 내 sequelize 쿼리는 db.js에서 호출 할 때 작동하지만 initializeDb (import 문)에서 참조하려고 할 때 길어진다. –