Replies: 1 comment 2 replies
-
Hello @warcraft14115 ! I extracted your product-fetching logic into a separate function const {
MongoClient
} = require("mongodb");
const mongoClient = new MongoClient(process.env.MONG_DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
export const productsCollection = async () => {
if (!mongoClient.isConnected()) await mongoClient.connect().then(res => {
return res.db("shop").collection("products");
});
};
const getProducts = async () => {
const result = await productsCollection();
result.find({}).toArray((error, result) => {
return result
})
}
getProducts().then(result => {
// Do something with result here
}) This means you can call the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to reuse Mongodb, I tried:
There codes will work ok. But How to make it more easier to reuse?
I expect it can reuse like this
productsCollection.find({}).toArray()
Beta Was this translation helpful? Give feedback.
All reactions