Report this

What is the reason for this report?

How to connect to db, then read collection into array, before using data in array

Posted on January 15, 2019

Promises and async/await somehow don’t work as I’m coding them. I must (1) connect to db, (2) read collections into arrays, then (3) use data in arrays, IN THAT ORDER. I have not figured out how to force MongoDB + Node.js + JavaScript to do that. It keeps trying to access collections before the db is really connected, and to use data in the arrays before they are really loaded from the collections.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

No idea if anyone can help you here but IMO it’s all about the logic within your code, a few conditionals should help you achieve this.

If DB connection = True then read the collection (select * from your table where date between etc.) > collection dict If collection dict > 1 then use the data in arrays[your stuff in here] else wait (add some logic flags in here) else throw error message (‘Can't connect’)

Don’t rely too much on my logic but this could be the big picture, there’s more needed to make it usable.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.