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

182
Visualizações
Retrieving value from Mongoose Promise as a callback

Having trouble pulling the data from a mongoose promise. I want to find a set of users from mongoose using an appropriate ID and passing the result along a promise chain.

The query is pulling the right data from the database. When I nest the query inside of a promise chain and then attempt to pass along the result, my console.log shows [Function (anonymous)]. Is there an appropriate structure for pulling the data from MongoDB using mongoose and then passing that data along a promise chain?

Promise chain:

addItem(guildId, items, timeCreated) {
    return new Promise((resolve, reject) => {
        // other functionality omitted for brevity
        resolve(guildId, items, timeCreated);
    }).then((guild, item, timeCreate) => {
        findGroupUsers(guild).then((response) => {
            return new Promise((resolve, reject) => {
                console.log(response.userId);
                resolve(response);
            });
        });

Mongoose query:


const findGroupUsers = async (guildId) => {
    const members = await EngagementTracking.find({ guildId: `${guildId}` }).exec();
    console.log(members);
    return members;
};

The verified answer successfully pulls the data from the query inside of addItem. To continue and pass the result of the query along chain, I did the following:

addItem(guildId, items, timeCreated) {
    return new Promise((resolve, reject) => {
//other functionality
        resolve(guildId, items, timeCreated);
    }).then((guild, items, timeCreate) => {
        return findGroupUsers(guild).then((response) => {
            return new Promise((resolve) => { resolve(response); });
        });
    }).then((data) =>{// other functionality //});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You function add item seem a bit strange you should do this instead:

addItem(guildId, items, timeCreated) {
    return new Promise((resolve, reject) => {
        // other functionality omitted for brevity
        resolve(guildId, items, timeCreated);
    }).then((guild, predict, timeCreate) => {
        return findGroupUsers(guild).then((response) => {
          console.log(response.userId);
          return response;
        });

you don't need to return a new promise inside a .then also be warn that you should return your promise (you didn't return findGroupUser)

at the end you should have a addItem.then(/do something/) or an await

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