首页 文章

使用Sequelize和Express进行API路由时出错

提问于
浏览
1

我在跟踪我的路由失败的原因时遇到了问题 . 我是Sequelize和Express路由的新手 .

目标 - 访问API endpoints '/v1/agent/:id'时,我想从Sequelize查询返回JSON响应 . 我已经确认查询有效,并将一行映射到我的 Agent 模型 .

当我启动应用程序时,我从Node获得了 Router.use() requires middleware function but got a ' + gettype(fn)); 异常 . 异常来自initializeDb函数,但我不知道为什么 .

这是根index.js:

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;

来自/ routes的我的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);
        });
  });
}

最后,模型agents.js

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

有人愿意第二眼看到这个吗?

1 回答

  • 1

    您必须从agent.js返回api对象,以便express.use在routes.js中正常工作 .

    这与Sequelize无关,所以我从我工作的例子中删除了它 . 看一看 .

    routes.js

    import express from 'express';
    import agent from './agent';
    
    // handle db configs
    let app = express();
    
    app.use('/agent', agent('config','database'));
    
    export default app;
    

    agent.js

    import {Router} from 'express';
    
    export default(config, db) => {
    
      let api = Router();
    
      api.get('/:id', (req, res, next) => {
        console.log('config', config);
        console.log('db', db);
        res.send('GET');
      });
    
      api.post('/:id', (req, res, next) => {
        console.log('config', config);
        console.log('db', db);
        res.send('POST');
      });
    
      return api;
    };
    

    控制台日志只是小心,因此您可以看到传递的值 .

相关问题