在nodejs中显示数据时出错[ERR_HTTP_HEADERS_SENT]

时间:2019-12-02 21:38:03

标签: javascript node.js api web

我正在尝试在表中显示API的必要数据,因为我正在使用nodejs和.ejs。将视图加载到浏览器中时,出现以下错误错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后,无法设置标头。这是负责加载动态的路线代码。ejs

router.get('/movimientos',  (req, res, next) => {
  dbConnection.query("SELECT * FROM [dbo].[WMS_Movimientos]", (err, result) => {
    console.log(result.recordsets[0].length)
    console.log(result)
    res.render('movimientos', {
      movTable : result
    });
  });
})

以下是查看代码


    <table id="dtBasicExample" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
        <thead>
          <tr>
            <th class="th-sm">Name

            </th>
            <th class="th-sm">Position

            </th>
            <th class="th-sm">Office

            </th>
            <th class="th-sm">Age

            </th>
            <th class="th-sm">Start date

            </th>
            <th class="th-sm">Salary

            </th>
          </tr>
        </thead>
        <tbody>
          <% for (var i = 0; i < movTable.recordsets[0].length; i ++) { %>
            <tr>
              <td><%= movTable.recordsets[0][i].Cod_Empresa %></td>
              <td><%= movTable.recordsets[0][i].Nro_Dcto %></td>
              <td><%= movTable.recordsets[0][i].Tipo_Dcto %></td>
              <td><%= movTable.recordsets[0][i].Cod_Barras %></td>
              <td><%= movTable.recordsets[0][i].Codigo %></td>
              <td><%= movTable.recordsets[0][i].Descripcion %></td>
              <td><%= movTable.recordsets[0][i].Cod_Bodega %></td>
              <td><%= movTable.recordsets[0][i].Cod_bodega_Origen %></td>
              <td><%= movTable.recordsets[0][i].Cod_Bodega_Destino %></td>
              <td><%= movTable.recordsets[0][i].Und_Medida %></td>
              <td><%= movTable.recordsets[0][i].Und_Empaque %></td>
              <td><%= movTable.recordsets[0][i].cantidad %></td>
              <td><%= movTable.recordsets[0][i].Observaciones %></td>
              <td><%= movTable.recordsets[0][i].Fecha %></td>
              <td><%= movTable.recordsets[0][i].Prioridad %></td>
              <td><%= movTable.recordsets[0][i].Nit_Responsable %></td>
              <td><%= movTable.recordsets[0][i].Cod_Usuario %></td>
              <td><%= movTable.recordsets[0][i].Cod_Centro_Costos %></td>
              <td><%= movTable.recordsets[0][i].Nro_Dcto_Cliente %></td>
              <td><%= movTable.recordsets[0][i].Fecha_procesado %></td>
              <td><%= movTable.recordsets[0][i].Estado %></td>
            </tr>
            <% } %>
        </tbody>
        <tfoot>
          <tr>
            <th>Name
            </th>
            <th>Position
            </th>
            <th>Office
            </th>
            <th>Age
            </th>
            <th>Start date
            </th>
            <th>Salary
            </th>
          </tr>
        </tfoot>
      </table>

我试图通过异步配置路由来解决错误,但是当我要执行代码时,它仍然显示相同的错误。

这是我的server.js代码

```
const express = require('express');
const engine = require('ejs-mate');
const path = require('path');
const morgan = require('morgan');
const passport = require('passport')
const session = require('express-session');
const flash = require('connect-flash');


;//Inizializando
const app = express();
require('./routes/database');
require('./passport/local-auth');


//middlewares




app.use(morgan('dev'));
app.use(express.urlencoded({extended: false}));
app.use(session({
    secret: 'secretsession',
    resave: false,
    saveUninitialized: false
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());

//settings
app.set('views', path.join(__dirname, 'views'));
app.engine('ejs', engine);
app.set('view engine', 'ejs');
app.set('port',process.env.PORT || 3000);


app.use((req, res, next) => {
    app.locals.signupMessage = req.flash('signupMessage');
    app.locals.signinMessage = req.flash('signinMessage');
    app.locals.user = req.user;
    next();
});

//Routes
app.use('/', require('./routes/index'));
require('./routes/index')

//Starting the server
app.listen(app.get('port'), () => {
    console.log("Server on port ", app.get('port'))
}); 

0 个答案:

没有答案