I am trying to include my collection of documents in an embed using MongoDB's ForEach function.
However, I have noticed that when attempting to add a field to the embed within the forEach loop, it appears that the code sends the message first and then adds the fields subsequently. This behavior is causing some fields to be skipped.
const Discord = require("discord.js");
const mongoDb = require("mongodb").MongoClient;
let showEmbed = new Discord.RichEmbed();
let proccess = 0;
module.exports.show = (message, page) => {
mongoDb.connect('mongodb+srv://admin:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33000203070673415c515c471e4b5a0a59471d5450431d5e5c5d545c57511d5d5647">[email protected]</a>/eco?retryWrites=true', {
useNewUrlParser: true
}, function (err, db) {
if (err) console.log(err);
let dbo = db.db("eco");
dbo.collection("items").find({
itemPage: page
}).forEach(function (doc) {
console.log(`${doc.itemName} : ${doc.itemPrice} : ${doc.itemDescription}`)
showEmbed.addField(`**${doc.itemName}** | $${doc.itemPrice}`, doc.itemDescription, false);
})
});
message.channel.send(showEmbed);
}