I understood why it was an error, I tried the suggestions like return or do function aysnc, but I could not solve it.
I have documents as long as find.lenght and 100 sub-documents. That's why I am use two loop. When first iteration is complete and second matching value is found, server is crashed. Without server it works.
I have documents called 0,1,2 and there are 100 crypto coins records from 0 to 100 in them. I added an image. For example, USDT is at [0,3], [1,43], [2,13] and I want to send all of three.
app.post('/', (req, res) => {
print(req, res)
})
function print(req, res) {
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("cryptoDb");
dbo.collection("coinTable").find({}).toArray(function (err, find) {
if (err) throw err;
for (i = 0; i < find.length; i++) {
let found = false;
for (j = 0; j < 100; j++) {
//console.log(i + " " + j)
let id = Capitalize(req.body.coinName);
if (find[i].result[j].name == id || find[i].result[j].symbol == id.toUpperCase()) {
// console.log(find[i].result[j]);
res.send(find[i].result[j]);
found = true;
}
}
if (!found) {
console.log("Not found")
}
}
db.close();
});
});
function Capitalize(s) {
return s[0].toUpperCase() + s.slice(1).toLowerCase();
}
}

