[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

后端 未结 3 955
广开言路
广开言路 2021-02-19 18:28

I\'m working with PostgreSQL and NodeJS with its \"PG Module\". CRUD works but sometimes doesn\'t update automatically the views when i save or delete some item. this is my cod

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 19:01

    Check res.send() should not call two times.

    In Controller

    const getAll = function(req, res){
       res.send(service.getAll(req,res));
      }
    

    In Service

    const Type = require("../models/type.model.js");
    exports.getAll = (req, res) => {
      Type.getAll((err, data) => {  
        res.send(data);
      });
    };
    

    Above res.send(data); two-time calling will create a problem. better to use

    const getAll = function(req, res){
            service.getAll(req,res);
          }
    

提交回复
热议问题