Node.js Node JS和Sequelize错误:发送到客户端后无法设置头

Node.js Node JS和Sequelize错误:发送到客户端后无法设置头,node.js,express,sequelize.js,Node.js,Express,Sequelize.js,当我试图通过查询将用户名从id输入控制器时,出现了这个错误。我试图解决这个问题,但没有解决办法,有没有办法通过查询获得用户名 用户型号: 'use strict'; const { Model } = require('sequelize'); const config = require('../config'); module.exports = (sequelize, DataTypes) => { class tb_user extends Model { sta

当我试图通过查询将用户名从id输入控制器时,出现了这个错误。我试图解决这个问题,但没有解决办法,有没有办法通过查询获得用户名

用户型号:

'use strict';
const {
  Model
} = require('sequelize');
const config = require('../config');
module.exports = (sequelize, DataTypes) => {
  class tb_user extends Model {

    static associate(models) {
    }
  };
  tb_user.init({
    id_user:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_user',
    freezeTableName:true
  });

  tb_user.associate = models =>{
    tb_user.hasMany(models.tb_tech,{
      foreignKey:'id_user',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_user;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_supervisor extends Model {

    static associate(models) {
    }
  };
  tb_supervisor.init({
    id_supervisor:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_supervisor',
    freezeTableName:true
  });

  tb_supervisor.associate = models =>{
    tb_supervisor.hasMany(models.tb_tech,{
      foreignKey:'id_supervisor',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_supervisor;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_tech extends Model {
    static associate(models) {
    }
  };
  tb_tech.init({
    id_tech:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    id_user: DataTypes.INTEGER,
    id_supervisor: DataTypes.INTEGER,
    zone:DataTypes.INTEGER,
    region:DataTypes.INTEGER,
    full_name: DataTypes.STRING,
    tech_type: DataTypes.STRING,
    goal: DataTypes.FLOAT,
    observations: DataTypes.STRING,
    client: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_tech',
    freezeTableName:true
  });

  tb_tech.associate = models =>{
    tb_tech.belongsTo(models.tb_user,{
      foreignKey:'id_user',
      as:'tb_user',
      onDelete:'CASCADE'
    })

    tb_tech.belongsTo(models.tb_supervisor,{
      foreignKey:'id_supervisor',
      as:'tb_supervisor',
      onDelete:'CASCADE'
    })
  }

  return tb_tech;
};
const Supervisor = require("../models").tb_supervisor;
const Tecnico = require("../models").tb_tech;
const Usuario = require("../models").tb_user;

module.exports = {
   getAllSupervisors(req,res){
        const id_supervisor = req.params.id_supervisor;
        return Supervisor
                .findAll({
                    include:[{
                        model:Tecnico, as:'tb_tech',
                        where:{
                            id_supervisor:id_supervisor
                        }
                    }]
                })
                .then(sup => {
                    const resObj = sup.map(supervisor => {
                        return Object.assign(
                            {},
                            {
                                id_supervisor : supervisor.id_supervisor,
                                username: supervisor.username,
                                email: supervisor.email,
                                technician: supervisor.tb_tech.map(tec => {
                                    return Object.assign(
                                        {},
                                        {
                                            tech_id : tec.id_tech,
                                            id_user: Usuario.findOne({
                                                where:{
                                                    id_user:tec.id_user
                                                },
                                                attributes:['username']
                                            }).then(user => res.json(user)).catch(error => console.log(error)),
                                            id_supervisor: tec.id_supervisor,
                                            zone: tec.zone,
                                            region: tec.region,
                                            full_name: tec.full_name,
                                            tech_type:tec.tech_type,
                                            goal: tec.goal,
                                            observations: tec.observations,
                                            client: tec.client
                                        }
                                    )
                                })
                            }
                        )
                    })
                    res.status(200).json(resObj);
                })
                .catch(error => {
                    res.status(500).send({message:error.message});
                })
    },
}
const router = require('express').Router();
const UsuarioController = require("../controllers/user");
const TecnicoController = require("../controllers/tech");
const SupervisorController = require("../controllers/supervisor");

router.use(function(req,res,next){
    res.header(
        "Access-Control-Allow-Headers",
        "x-access-token, Origin, Content-Type, Accept",
        "Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE",
        "Access-Control-Allow-Origin", "*"
    );
    next();
});

router.get('/users/getAllUsers/:id_user',UsuarioController.getAllUsers);

router.get('/supervisor/getAllSupervisors/:id_supervisor',SupervisorController.getAllSupervisors);

router.get('/techs/getAllTechs',TecnicoController.getAllTechs);

module.exports = router;
主管型号:

'use strict';
const {
  Model
} = require('sequelize');
const config = require('../config');
module.exports = (sequelize, DataTypes) => {
  class tb_user extends Model {

    static associate(models) {
    }
  };
  tb_user.init({
    id_user:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_user',
    freezeTableName:true
  });

  tb_user.associate = models =>{
    tb_user.hasMany(models.tb_tech,{
      foreignKey:'id_user',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_user;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_supervisor extends Model {

    static associate(models) {
    }
  };
  tb_supervisor.init({
    id_supervisor:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_supervisor',
    freezeTableName:true
  });

  tb_supervisor.associate = models =>{
    tb_supervisor.hasMany(models.tb_tech,{
      foreignKey:'id_supervisor',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_supervisor;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_tech extends Model {
    static associate(models) {
    }
  };
  tb_tech.init({
    id_tech:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    id_user: DataTypes.INTEGER,
    id_supervisor: DataTypes.INTEGER,
    zone:DataTypes.INTEGER,
    region:DataTypes.INTEGER,
    full_name: DataTypes.STRING,
    tech_type: DataTypes.STRING,
    goal: DataTypes.FLOAT,
    observations: DataTypes.STRING,
    client: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_tech',
    freezeTableName:true
  });

  tb_tech.associate = models =>{
    tb_tech.belongsTo(models.tb_user,{
      foreignKey:'id_user',
      as:'tb_user',
      onDelete:'CASCADE'
    })

    tb_tech.belongsTo(models.tb_supervisor,{
      foreignKey:'id_supervisor',
      as:'tb_supervisor',
      onDelete:'CASCADE'
    })
  }

  return tb_tech;
};
const Supervisor = require("../models").tb_supervisor;
const Tecnico = require("../models").tb_tech;
const Usuario = require("../models").tb_user;

module.exports = {
   getAllSupervisors(req,res){
        const id_supervisor = req.params.id_supervisor;
        return Supervisor
                .findAll({
                    include:[{
                        model:Tecnico, as:'tb_tech',
                        where:{
                            id_supervisor:id_supervisor
                        }
                    }]
                })
                .then(sup => {
                    const resObj = sup.map(supervisor => {
                        return Object.assign(
                            {},
                            {
                                id_supervisor : supervisor.id_supervisor,
                                username: supervisor.username,
                                email: supervisor.email,
                                technician: supervisor.tb_tech.map(tec => {
                                    return Object.assign(
                                        {},
                                        {
                                            tech_id : tec.id_tech,
                                            id_user: Usuario.findOne({
                                                where:{
                                                    id_user:tec.id_user
                                                },
                                                attributes:['username']
                                            }).then(user => res.json(user)).catch(error => console.log(error)),
                                            id_supervisor: tec.id_supervisor,
                                            zone: tec.zone,
                                            region: tec.region,
                                            full_name: tec.full_name,
                                            tech_type:tec.tech_type,
                                            goal: tec.goal,
                                            observations: tec.observations,
                                            client: tec.client
                                        }
                                    )
                                })
                            }
                        )
                    })
                    res.status(200).json(resObj);
                })
                .catch(error => {
                    res.status(500).send({message:error.message});
                })
    },
}
const router = require('express').Router();
const UsuarioController = require("../controllers/user");
const TecnicoController = require("../controllers/tech");
const SupervisorController = require("../controllers/supervisor");

router.use(function(req,res,next){
    res.header(
        "Access-Control-Allow-Headers",
        "x-access-token, Origin, Content-Type, Accept",
        "Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE",
        "Access-Control-Allow-Origin", "*"
    );
    next();
});

router.get('/users/getAllUsers/:id_user',UsuarioController.getAllUsers);

router.get('/supervisor/getAllSupervisors/:id_supervisor',SupervisorController.getAllSupervisors);

router.get('/techs/getAllTechs',TecnicoController.getAllTechs);

module.exports = router;
技师型号:

'use strict';
const {
  Model
} = require('sequelize');
const config = require('../config');
module.exports = (sequelize, DataTypes) => {
  class tb_user extends Model {

    static associate(models) {
    }
  };
  tb_user.init({
    id_user:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_user',
    freezeTableName:true
  });

  tb_user.associate = models =>{
    tb_user.hasMany(models.tb_tech,{
      foreignKey:'id_user',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_user;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_supervisor extends Model {

    static associate(models) {
    }
  };
  tb_supervisor.init({
    id_supervisor:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_supervisor',
    freezeTableName:true
  });

  tb_supervisor.associate = models =>{
    tb_supervisor.hasMany(models.tb_tech,{
      foreignKey:'id_supervisor',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_supervisor;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_tech extends Model {
    static associate(models) {
    }
  };
  tb_tech.init({
    id_tech:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    id_user: DataTypes.INTEGER,
    id_supervisor: DataTypes.INTEGER,
    zone:DataTypes.INTEGER,
    region:DataTypes.INTEGER,
    full_name: DataTypes.STRING,
    tech_type: DataTypes.STRING,
    goal: DataTypes.FLOAT,
    observations: DataTypes.STRING,
    client: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_tech',
    freezeTableName:true
  });

  tb_tech.associate = models =>{
    tb_tech.belongsTo(models.tb_user,{
      foreignKey:'id_user',
      as:'tb_user',
      onDelete:'CASCADE'
    })

    tb_tech.belongsTo(models.tb_supervisor,{
      foreignKey:'id_supervisor',
      as:'tb_supervisor',
      onDelete:'CASCADE'
    })
  }

  return tb_tech;
};
const Supervisor = require("../models").tb_supervisor;
const Tecnico = require("../models").tb_tech;
const Usuario = require("../models").tb_user;

module.exports = {
   getAllSupervisors(req,res){
        const id_supervisor = req.params.id_supervisor;
        return Supervisor
                .findAll({
                    include:[{
                        model:Tecnico, as:'tb_tech',
                        where:{
                            id_supervisor:id_supervisor
                        }
                    }]
                })
                .then(sup => {
                    const resObj = sup.map(supervisor => {
                        return Object.assign(
                            {},
                            {
                                id_supervisor : supervisor.id_supervisor,
                                username: supervisor.username,
                                email: supervisor.email,
                                technician: supervisor.tb_tech.map(tec => {
                                    return Object.assign(
                                        {},
                                        {
                                            tech_id : tec.id_tech,
                                            id_user: Usuario.findOne({
                                                where:{
                                                    id_user:tec.id_user
                                                },
                                                attributes:['username']
                                            }).then(user => res.json(user)).catch(error => console.log(error)),
                                            id_supervisor: tec.id_supervisor,
                                            zone: tec.zone,
                                            region: tec.region,
                                            full_name: tec.full_name,
                                            tech_type:tec.tech_type,
                                            goal: tec.goal,
                                            observations: tec.observations,
                                            client: tec.client
                                        }
                                    )
                                })
                            }
                        )
                    })
                    res.status(200).json(resObj);
                })
                .catch(error => {
                    res.status(500).send({message:error.message});
                })
    },
}
const router = require('express').Router();
const UsuarioController = require("../controllers/user");
const TecnicoController = require("../controllers/tech");
const SupervisorController = require("../controllers/supervisor");

router.use(function(req,res,next){
    res.header(
        "Access-Control-Allow-Headers",
        "x-access-token, Origin, Content-Type, Accept",
        "Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE",
        "Access-Control-Allow-Origin", "*"
    );
    next();
});

router.get('/users/getAllUsers/:id_user',UsuarioController.getAllUsers);

router.get('/supervisor/getAllSupervisors/:id_supervisor',SupervisorController.getAllSupervisors);

router.get('/techs/getAllTechs',TecnicoController.getAllTechs);

module.exports = router;
主管控制器:

'use strict';
const {
  Model
} = require('sequelize');
const config = require('../config');
module.exports = (sequelize, DataTypes) => {
  class tb_user extends Model {

    static associate(models) {
    }
  };
  tb_user.init({
    id_user:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_user',
    freezeTableName:true
  });

  tb_user.associate = models =>{
    tb_user.hasMany(models.tb_tech,{
      foreignKey:'id_user',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_user;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_supervisor extends Model {

    static associate(models) {
    }
  };
  tb_supervisor.init({
    id_supervisor:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_supervisor',
    freezeTableName:true
  });

  tb_supervisor.associate = models =>{
    tb_supervisor.hasMany(models.tb_tech,{
      foreignKey:'id_supervisor',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_supervisor;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_tech extends Model {
    static associate(models) {
    }
  };
  tb_tech.init({
    id_tech:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    id_user: DataTypes.INTEGER,
    id_supervisor: DataTypes.INTEGER,
    zone:DataTypes.INTEGER,
    region:DataTypes.INTEGER,
    full_name: DataTypes.STRING,
    tech_type: DataTypes.STRING,
    goal: DataTypes.FLOAT,
    observations: DataTypes.STRING,
    client: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_tech',
    freezeTableName:true
  });

  tb_tech.associate = models =>{
    tb_tech.belongsTo(models.tb_user,{
      foreignKey:'id_user',
      as:'tb_user',
      onDelete:'CASCADE'
    })

    tb_tech.belongsTo(models.tb_supervisor,{
      foreignKey:'id_supervisor',
      as:'tb_supervisor',
      onDelete:'CASCADE'
    })
  }

  return tb_tech;
};
const Supervisor = require("../models").tb_supervisor;
const Tecnico = require("../models").tb_tech;
const Usuario = require("../models").tb_user;

module.exports = {
   getAllSupervisors(req,res){
        const id_supervisor = req.params.id_supervisor;
        return Supervisor
                .findAll({
                    include:[{
                        model:Tecnico, as:'tb_tech',
                        where:{
                            id_supervisor:id_supervisor
                        }
                    }]
                })
                .then(sup => {
                    const resObj = sup.map(supervisor => {
                        return Object.assign(
                            {},
                            {
                                id_supervisor : supervisor.id_supervisor,
                                username: supervisor.username,
                                email: supervisor.email,
                                technician: supervisor.tb_tech.map(tec => {
                                    return Object.assign(
                                        {},
                                        {
                                            tech_id : tec.id_tech,
                                            id_user: Usuario.findOne({
                                                where:{
                                                    id_user:tec.id_user
                                                },
                                                attributes:['username']
                                            }).then(user => res.json(user)).catch(error => console.log(error)),
                                            id_supervisor: tec.id_supervisor,
                                            zone: tec.zone,
                                            region: tec.region,
                                            full_name: tec.full_name,
                                            tech_type:tec.tech_type,
                                            goal: tec.goal,
                                            observations: tec.observations,
                                            client: tec.client
                                        }
                                    )
                                })
                            }
                        )
                    })
                    res.status(200).json(resObj);
                })
                .catch(error => {
                    res.status(500).send({message:error.message});
                })
    },
}
const router = require('express').Router();
const UsuarioController = require("../controllers/user");
const TecnicoController = require("../controllers/tech");
const SupervisorController = require("../controllers/supervisor");

router.use(function(req,res,next){
    res.header(
        "Access-Control-Allow-Headers",
        "x-access-token, Origin, Content-Type, Accept",
        "Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE",
        "Access-Control-Allow-Origin", "*"
    );
    next();
});

router.get('/users/getAllUsers/:id_user',UsuarioController.getAllUsers);

router.get('/supervisor/getAllSupervisors/:id_supervisor',SupervisorController.getAllSupervisors);

router.get('/techs/getAllTechs',TecnicoController.getAllTechs);

module.exports = router;
路线:

'use strict';
const {
  Model
} = require('sequelize');
const config = require('../config');
module.exports = (sequelize, DataTypes) => {
  class tb_user extends Model {

    static associate(models) {
    }
  };
  tb_user.init({
    id_user:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_user',
    freezeTableName:true
  });

  tb_user.associate = models =>{
    tb_user.hasMany(models.tb_tech,{
      foreignKey:'id_user',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_user;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_supervisor extends Model {

    static associate(models) {
    }
  };
  tb_supervisor.init({
    id_supervisor:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_supervisor',
    freezeTableName:true
  });

  tb_supervisor.associate = models =>{
    tb_supervisor.hasMany(models.tb_tech,{
      foreignKey:'id_supervisor',
      as:'tb_tech',
      onDelete:'CASCADE'
    })
  }
  return tb_supervisor;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class tb_tech extends Model {
    static associate(models) {
    }
  };
  tb_tech.init({
    id_tech:{
      type:DataTypes.INTEGER,
      primaryKey:true,
      autoIncrement:true
    },
    id_user: DataTypes.INTEGER,
    id_supervisor: DataTypes.INTEGER,
    zone:DataTypes.INTEGER,
    region:DataTypes.INTEGER,
    full_name: DataTypes.STRING,
    tech_type: DataTypes.STRING,
    goal: DataTypes.FLOAT,
    observations: DataTypes.STRING,
    client: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'tb_tech',
    freezeTableName:true
  });

  tb_tech.associate = models =>{
    tb_tech.belongsTo(models.tb_user,{
      foreignKey:'id_user',
      as:'tb_user',
      onDelete:'CASCADE'
    })

    tb_tech.belongsTo(models.tb_supervisor,{
      foreignKey:'id_supervisor',
      as:'tb_supervisor',
      onDelete:'CASCADE'
    })
  }

  return tb_tech;
};
const Supervisor = require("../models").tb_supervisor;
const Tecnico = require("../models").tb_tech;
const Usuario = require("../models").tb_user;

module.exports = {
   getAllSupervisors(req,res){
        const id_supervisor = req.params.id_supervisor;
        return Supervisor
                .findAll({
                    include:[{
                        model:Tecnico, as:'tb_tech',
                        where:{
                            id_supervisor:id_supervisor
                        }
                    }]
                })
                .then(sup => {
                    const resObj = sup.map(supervisor => {
                        return Object.assign(
                            {},
                            {
                                id_supervisor : supervisor.id_supervisor,
                                username: supervisor.username,
                                email: supervisor.email,
                                technician: supervisor.tb_tech.map(tec => {
                                    return Object.assign(
                                        {},
                                        {
                                            tech_id : tec.id_tech,
                                            id_user: Usuario.findOne({
                                                where:{
                                                    id_user:tec.id_user
                                                },
                                                attributes:['username']
                                            }).then(user => res.json(user)).catch(error => console.log(error)),
                                            id_supervisor: tec.id_supervisor,
                                            zone: tec.zone,
                                            region: tec.region,
                                            full_name: tec.full_name,
                                            tech_type:tec.tech_type,
                                            goal: tec.goal,
                                            observations: tec.observations,
                                            client: tec.client
                                        }
                                    )
                                })
                            }
                        )
                    })
                    res.status(200).json(resObj);
                })
                .catch(error => {
                    res.status(500).send({message:error.message});
                })
    },
}
const router = require('express').Router();
const UsuarioController = require("../controllers/user");
const TecnicoController = require("../controllers/tech");
const SupervisorController = require("../controllers/supervisor");

router.use(function(req,res,next){
    res.header(
        "Access-Control-Allow-Headers",
        "x-access-token, Origin, Content-Type, Accept",
        "Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE",
        "Access-Control-Allow-Origin", "*"
    );
    next();
});

router.get('/users/getAllUsers/:id_user',UsuarioController.getAllUsers);

router.get('/supervisor/getAllSupervisors/:id_supervisor',SupervisorController.getAllSupervisors);

router.get('/techs/getAllTechs',TecnicoController.getAllTechs);

module.exports = router;
在postman中,查询很好,但是我需要用户名的字段是一个空白对象,控制台抛出以下错误。可以在另一个查询中进行查询吗

图像:

问题是,错误的来源是什么?你能在另一个查询中进行查询吗


事先非常感谢。

问题出在您的主管控制器上


.then(sup => {
    const resObj = sup.map(supervisor => { // POINT ---- 1
        return Object.assign(
            {},
            {
                id_supervisor : supervisor.id_supervisor,
                username: supervisor.username,
                email: supervisor.email,
                technician: supervisor.tb_tech.map(tec => {
                    return Object.assign(
                        {},
                        {
                            tech_id : tec.id_tech,
                            id_user: Usuario.findOne({ // POINT --------- 2
                                where:{
                                    id_user:tec.id_user
                                },
                                attributes:['username']
                            }).then(user => res.json(user) // POINT 4).catch(error => console.log(error)),
                            id_supervisor: tec.id_supervisor,
                            zone: tec.zone,
                            region: tec.region,
                            full_name: tec.full_name,
                            tech_type:tec.tech_type,
                            goal: tec.goal,
                            observations: tec.observations,
                            client: tec.client
                        }
                    )
                })
            }
        )
    })
    res.status(200).json(resObj);  // POINT  ------ 3
})
              
第二点:您的
Usuario.findOne
是DB查询,它是异步的,这意味着它需要时间。 因此,您的第1点
sup.map(supervisor=>{
将在异步执行中转到,nodejs将在下一行继续工作

因此,第3点将在下一个事件循环中执行。因此,在执行第3点时,您的
resObj
为null,并表示返回的请求响应。 但在执行第4点时,您有一个return语句(这是另一个return语句),在一个请求中不可能有2个返回


检查NodeJ的异步性质并正确使用承诺。

问题在于您的主管控制器


.then(sup => {
    const resObj = sup.map(supervisor => { // POINT ---- 1
        return Object.assign(
            {},
            {
                id_supervisor : supervisor.id_supervisor,
                username: supervisor.username,
                email: supervisor.email,
                technician: supervisor.tb_tech.map(tec => {
                    return Object.assign(
                        {},
                        {
                            tech_id : tec.id_tech,
                            id_user: Usuario.findOne({ // POINT --------- 2
                                where:{
                                    id_user:tec.id_user
                                },
                                attributes:['username']
                            }).then(user => res.json(user) // POINT 4).catch(error => console.log(error)),
                            id_supervisor: tec.id_supervisor,
                            zone: tec.zone,
                            region: tec.region,
                            full_name: tec.full_name,
                            tech_type:tec.tech_type,
                            goal: tec.goal,
                            observations: tec.observations,
                            client: tec.client
                        }
                    )
                })
            }
        )
    })
    res.status(200).json(resObj);  // POINT  ------ 3
})
              
第二点:您的
Usuario.findOne
是DB查询,它是异步的,这意味着它需要时间。 因此,您的第1点
sup.map(supervisor=>{
将在异步执行中转到,nodejs将在下一行继续工作

因此,第3点将在下一个事件循环中执行。因此,在执行第3点时,您的
resObj
为null,并表示返回的请求响应。 但在执行第4点时,您有一个return语句(这是另一个return语句),在一个请求中不可能有2个返回


检查NodeJ的异步性质并正确使用承诺。

顾名思义,这是因为您尝试多次发送响应头,即在第一和第二位置发送响应头,这是不可能的,请尝试根据NodeJ的异步行为更改代码使用async Wait以避免函数链接避免使用asy在数组的map、filter和其他异步函数中进行nc调用,因为它们不等待使用本机或for of循环

module.exports = {
  getAllSupervisors(req, res) {
    const id_supervisor = req.params.id_supervisor;
    return Supervisor.findAll({
      include: [
        {
          model: Tecnico,
          as: "tb_tech",
          where: {
            id_supervisor: id_supervisor
          }
        }
      ]
    })
      .then((sup) => {
        const resObj = sup.map((supervisor) => {
          return Object.assign(
            {},
            {
              id_supervisor: supervisor.id_supervisor,
              username: supervisor.username,
              email: supervisor.email,
              technician: supervisor.tb_tech.map((tec) => {
                return Object.assign(
                  {},
                  {
                    tech_id: tec.id_tech,
                    id_user: Usuario.findOne({
                      where: {
                        id_user: tec.id_user
                      },
                      attributes: ["username"]
                    })
                      .then((user) => res.json(user)) // sending response here for first time
                      .catch((error) => console.log(error)),
                    id_supervisor: tec.id_supervisor,
                    zone: tec.zone,
                    region: tec.region,
                    full_name: tec.full_name,
                    tech_type: tec.tech_type,
                    goal: tec.goal,
                    observations: tec.observations,
                    client: tec.client
                  }
                );
              })
            }
          );
        });
        res.status(200).json(resObj); //for second time
      })
      .catch((error) => {
        res.status(500).send({ message: error.message });
      });
  }
};
要避免这种情况,请尝试这样做

module.exports = {
  async getAllSupervisors(req, res) {
    const id_supervisor = req.params.id_supervisor;
    const sub = await Supervisor.findAll({
      include: [
        {
          model: Tecnico,
          as: "tb_tech",
          where: {
            id_supervisor: id_supervisor
          }
        }
      ]
    });
    // const resObj = sup.map((supervisor) => {
    const resObj = [];
    for (const supervisor of sub) {
      resObj.push(
        Object.assign(
          {},
          {
            id_supervisor: supervisor.id_supervisor,
            username: supervisor.username,
            email: supervisor.email,
            technician: supervisor.tb_tech.map((tec) => {
              return Object.assign(
                {},
                {
                  tech_id: tec.id_tech,
                  id_user: await Usuario.findOne({
                    where: {
                      id_user: tec.id_user
                    },
                    attributes: ["username"]
                  }),
                  id_supervisor: tec.id_supervisor,
                  zone: tec.zone,
                  region: tec.region,
                  full_name: tec.full_name,
                  tech_type: tec.tech_type,
                  goal: tec.goal,
                  observations: tec.observations,
                  client: tec.client
                }
              );
            })
          }
        )
      );
    }
    // });
    res.status(200).json(resObj); //second
    res.status(500).send({ message: error.message });
  }
};


顾名思义,之所以会发生这种情况,是因为您尝试多次发送响应头,即在第一位和第二位发送响应头,这是不可能的,请尝试根据节点的异步行为更改代码使用async Wait以避免函数链接避免在映射、筛选器和数组的其他异步函数中进行异步调用它们不是等待使用本机或循环的

module.exports = {
  getAllSupervisors(req, res) {
    const id_supervisor = req.params.id_supervisor;
    return Supervisor.findAll({
      include: [
        {
          model: Tecnico,
          as: "tb_tech",
          where: {
            id_supervisor: id_supervisor
          }
        }
      ]
    })
      .then((sup) => {
        const resObj = sup.map((supervisor) => {
          return Object.assign(
            {},
            {
              id_supervisor: supervisor.id_supervisor,
              username: supervisor.username,
              email: supervisor.email,
              technician: supervisor.tb_tech.map((tec) => {
                return Object.assign(
                  {},
                  {
                    tech_id: tec.id_tech,
                    id_user: Usuario.findOne({
                      where: {
                        id_user: tec.id_user
                      },
                      attributes: ["username"]
                    })
                      .then((user) => res.json(user)) // sending response here for first time
                      .catch((error) => console.log(error)),
                    id_supervisor: tec.id_supervisor,
                    zone: tec.zone,
                    region: tec.region,
                    full_name: tec.full_name,
                    tech_type: tec.tech_type,
                    goal: tec.goal,
                    observations: tec.observations,
                    client: tec.client
                  }
                );
              })
            }
          );
        });
        res.status(200).json(resObj); //for second time
      })
      .catch((error) => {
        res.status(500).send({ message: error.message });
      });
  }
};
要避免这种情况,请尝试这样做

module.exports = {
  async getAllSupervisors(req, res) {
    const id_supervisor = req.params.id_supervisor;
    const sub = await Supervisor.findAll({
      include: [
        {
          model: Tecnico,
          as: "tb_tech",
          where: {
            id_supervisor: id_supervisor
          }
        }
      ]
    });
    // const resObj = sup.map((supervisor) => {
    const resObj = [];
    for (const supervisor of sub) {
      resObj.push(
        Object.assign(
          {},
          {
            id_supervisor: supervisor.id_supervisor,
            username: supervisor.username,
            email: supervisor.email,
            technician: supervisor.tb_tech.map((tec) => {
              return Object.assign(
                {},
                {
                  tech_id: tec.id_tech,
                  id_user: await Usuario.findOne({
                    where: {
                      id_user: tec.id_user
                    },
                    attributes: ["username"]
                  }),
                  id_supervisor: tec.id_supervisor,
                  zone: tec.zone,
                  region: tec.region,
                  full_name: tec.full_name,
                  tech_type: tec.tech_type,
                  goal: tec.goal,
                  observations: tec.observations,
                  client: tec.client
                }
              );
            })
          }
        )
      );
    }
    // });
    res.status(200).json(resObj); //second
    res.status(500).send({ message: error.message });
  }
};