0

Sorry if my title is not very explicit I dont know how to explain this properly. I am trying to use the distinct function for my app that uses loopback 3 and mongodb. It seems to work right but my endpoint wont hit the return inside my function. This is my code

const distinctUsers = await  sellerCollection.distinct('userId',{
      hostId : host.id,
      eventId:{
        "$ne" : eventId
      }
    }, async function (err, userIds) {;

      if(!userIds || userIds.length ==0)
        return [];

      const filter = {
        where:{
          id: {
            inq: userIds
          }
        }
      };
      console.log("should be last")
      return await BPUser.find(filter);
    });
    console.log(distinctUsers);
    console.log("wtf??");
    //return [];

If I uncomment the return [] it will send the return and later it will show the should be last, so even when I dont have the return it seems to finish. It is now waiting for the response. I dont like the way my code looks so any pointer of how to make this look better I will take it.

2
  • 1
    What exactly is that sellerCollection thing? If it does not expect an async function, stuff probably won't work. Commented Aug 14, 2019 at 0:05
  • 1
    sellerCollection.distinct is highly unlikely to take a node-style asyncrhonous callback AND return a Promise - very rare to see something like that - also, you have a stray ; in the code Commented Aug 14, 2019 at 0:07

1 Answer 1

1

It looks like the sellerCollection.distinct takes a callback as one of it's parameters, therefore, you cannot use async/await with a callback-style function, since it's not a promise.

I would suggest turning this call into a promise if you'd like to use async/await:

function findDistinct(hostId, eventId) {
  return new Promise((resolve, reject) => {
    sellerCollection.distinct(
      'userId', 
      { hostId, eventId: { "$ne": eventId } },
      function (error, userIds) {
        if (error) { 
          reject(error); 
          return; 
        }
        if (!userIds || userIds.length === 0) {
          resolve([]);
          return;
        }
        resolve(userIds);
      }
    )
  })
}

Then, you can use this new function with async/await like such:

async function getDistinctUsers() {
  try {
    const hostId = ...
    const eventId = ...

    const distinctUsers = await findDistinct(hostId, eventId)

    if (distinctUsers.length === 0) {
      return
    }

    const filter = {
      where: {
        id: { inq: userIds }
      }
    }
    const bpUsers = await BPUser.find(filter) // assuming it's a promise

    console.log(bpUsers)
  } catch (error) {
    // handle error
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi I thought this would work but it still seems like what ever happens on the secondary function seems to run on a another thread idk. I did this console.log("a1"); const distinctUsers = await findDistinct (sellerCollection, host.id, eventId); console.log({"aaa": distinctUsers}); And it doesnt pass to the second console.log.
@JuanDiego I just realized I made a mistake in the findDistinct function, sorry, please try again with the changes that I made

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.