Hey everyone! I’m stuck with a database API in my Node.js project. I’ve got two tables: one for advisors and another for positions. I’m trying to get the position name for each advisor using a search function. But when I try to send the data to my view, I keep getting a ‘Promise Pending’ error. Here’s a simplified version of my code:
I’ve run into a similar situation before, and the main issue was with the asynchronous callbacks that don’t wait for all the data, which leads to the Promise remaining unresolved. From my experience, wrapping your advisor lookups in promises and then using Promise.all to wait for all the asynchronous calls to complete is essential. Instead of using a forEach loop with individual callbacks, returning a promise from your fetch function helps ensure that you only proceed once all the data is ready. A refactored version using async/await might look like this:
This approach ensures that all asynchronous operations complete before you try to render your view, which should resolve the ‘Promise Pending’ error you’re encountering.
I’ve run into similar issues before. The heart of the problem is that your fetchAdvisors function does not wait for the asynchronous positionTable.find calls to complete before returning, which leaves your promise unresolved. One effective solution is to modify fetchAdvisors so that it returns a promise that only resolves once all position lookups are finished. Wrapping each lookup in a promise and using Promise.all ensures that you collect all the updated advisors before proceeding. For example:
This approach should resolve the ‘Promise Pending’ error by ensuring that all asynchronous operations complete before the data is used to render your view.
hey jess, looks like ur issue is with the async nature of the positionTable.find() callback. try using Promise.all() to wait for all position lookups to complete before returning the advisors. also, make sure fetchAdvisors() actually returns the advisors array. hope this helps!