Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
How to export function as function expression in express node js

I have a function

exports.loginService = async(req,res) => {
  const { workerId, workerPassword } = req.body;
  try {
    const foundUser = await User.findOne({ workerId });
    if (!foundUser) {
      return res.status(404).json({
        message: "Employee does not exist (Invalid Employee ID)",
      });
    } else if (foundUser.totalLoginAttemptLeft > 0) {
      const { updatedWorker, isMatch } = await checkPassword(
        workerPassword,
        foundUser
      );
      if (!isMatch) {
        passwordNotMatched(res, updatedWorker);
      } else {
        const jwtToken = await totalLogin(foundUser, reset);
        return res.status(200).json({ token: "Bearer " + jwtToken });
      }
    } else {
      return res.status(400).json({
        message: "You'r account is locked Please contact to ADMIN or HR",
      });
    }
  } catch (error) {
    console.error(error.message);
    return res.status(500).json({ message: error.message });
  }
}

I am exporting this in the main route file

const loginService = require("../../services/authServices")

router.post("/login", loginService);

But when I running the code it's giving this error

Error: Route.post() requires a callback function but got a [object] Object

What I am doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

your file exports an object containing a function called loginService, so you should do something like:

const authServices = require("../../services/authServices")

router.post("/login", authServices.loginService);
about 4 years ago · Juan Pablo Isaza Relatório

0

You're trying to use the exports object as though it were a function. If you want to only export the function, overwrite exports rather than assigning to a property on it:

module.exports = async(req,res) => {
// ...
};

(Note that it has to be module.exports, not just exports.)

Alternatively, keep your current export and destructure the require call:

const { loginService } = require("../../services/authServices");
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda