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

340
Visualizações
NodeJS: Unhandled Promise Rejection at the end of a method

At the moment, I'm working on a RESTful-API with express and mongoose and I now have a problem.

First, my method:

public create() : Promise<UserDocument> {
    return new Promise((user) => {
        User.exists(this.username).then((exists) => {
            if (exists) {
                throw Errors.mongoose.user_already_exists;
            } else {
                UserModel.create(this.toIUser()).then((result) => {
                    user(result);
                }).catch(() => {
                    throw Errors.mongoose.user_create
                });
            }
        }).catch((error) => {
            throw error;
        })
    });
}

I get a unhandled promise rejections when I execute this method. This happens even if I handle the error when I execute the method like this:

User.fromIUser(user).create().then(() => {
    return response.status(200).send({
        message: "Created",
        user
    });
}).catch((error) => {
    return response.status(500).send({
        message: error
    });
});

Full stacktrace:

(node:23992) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): User already exists
(node:23992) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

How i can avoid this situation?

Thanks for your help, felixoi

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I found the solution! Just use "resolve, request" for creating a promise.

Here is now my method:

public create() : Promise<any> {
    return new Promise((resolve, reject) => {
        User.exists(this.username).then((exists) => {
            if (exists) {
                reject( Errors.mongoose.user_already_exists);
            } else {
                UserModel.create(this.toIUser()).then((result) => {
                    resolve(result);
                }).catch(() => {
                    reject(Errors.mongoose.user_create);
                });
            }
        }).catch((error) => {
            reject(error);
        })
    })
}

If you call the method now you can use the catch() method and everything works! Call it like this:

User.fromIUser(user).create().then((user) => {
    return response.status(200).send({
        message: "Created",
        user
    });
}).catch((error) => {
    return response.status(500).send({
        message: error
    })
})
about 4 years ago · Santiago Trujillo 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