Created
August 12, 2020 07:26
-
-
Save bengrunfeld/48eecad69f0d2dc9c08ce8f2c4f55676 to your computer and use it in GitHub Desktop.
Use Node.JS to read all records from a MongoDB Collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readAllRecords = db => { | |
const p = new Promise((resolve, reject) => { | |
db.collection(process.env.COL_NAME) | |
.find({}) | |
.toArray((error, results) => { | |
if (error) { | |
reject({ origin: "readAllRecords", error }); | |
return; | |
} | |
resolve(results); | |
}); | |
}); | |
p.catch(error => console.log("Error in readAllRecords:", error)); | |
return p; | |
}; | |
export default readAllRecords; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment